douyoufan7881 2017-06-12 06:31
浏览 73

通过MySQL查询在Wordpress中的独特slu ..

I want a MySQL query to find and replace keyword in wordpress database. And want to keep slugs unique. For example : If there are 3 slugs in the wp_terms table :-

  1. testkeyword
  2. testkeyword-2
  3. testslg

now I want to replace keyword slg with keyword and run the update query as

update wp_terms set `slug` = replace(slug, 'slg','keyword')

Now the third slug which was testslg will become testkeyword, and it will not remain unique, as the first slug exists same as the third.

How can I put a number at the end of slug if it already exists in a column? As in this example I want the third slug to be testkeyword-3. How is this possible?

  • 写回答

1条回答 默认 最新

  • drwpbrv668670 2017-06-12 07:10
    关注

    At the start get the initial count of your newly formed keyword. Suppose you are going to replace your third keyword i.e. testslg with testkeyword using following statement

    $newkeyword = str_replace("slg","keyword","testslg");
    

    Now, get the count of this newkeyword using following query

    SELECT count(slug) FROM wp_terms WHERE slug like '%$newkeyword%';
    

    Use this count in the update query

    update wp_terms set `slug` = '$newkeyword-$above_query_count_result' WHERE row_unique_id;
    

    Note: This answer is just the logic, please include all concatenation in your php code.

    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能