限制搜索和替换为类别

时间:2013-11-24 04:19:38

标签: php mysql wordpress phpmyadmin

我希望在主要网站上找到并替换某个关键字。

我通常会使用像

这样的东西
UPDATE wp_posts 
   SET post_content = REPLACE (post_content, 'Item to replace here', 'Replacement text here');  

但是,我不希望更新跨越整个网站,我希望将其限制为只有一个类别。

谁能告诉我如何实现这个目标?

1 个答案:

答案 0 :(得分:6)

更新:更准确地说,查询应该是

UPDATE wp_term_relationships r JOIN wp_posts p 
    ON r.object_id = p.id JOIN wp_terms t
    ON r.term_taxonomy_id = t.term_id JOIN wp_term_taxonomy x
    ON r.term_taxonomy_id = x.term_taxonomy_id
   SET p.post_content = REPLACE(post_content, 'Item to replace here', 'Replacement text here')
 WHERE t.name = 'My Category'
   AND x.taxonomy = 'category'
   AND x.parent = 0 -- top level category
更新任何内容之前

注意:

1)您可以通过发布

来查看哪些帖子会受到影响
SELECT *
  FROM wp_term_relationships r JOIN wp_posts p 
    ON r.object_id = p.id JOIN wp_terms t
    ON r.term_taxonomy_id = t.term_id JOIN wp_term_taxonomy x
    ON r.term_taxonomy_id = x.term_taxonomy_id
 WHERE t.name = 'My Category'
   AND x.taxonomy = 'category'
   AND x.parent = 0  -- top level category

2)确保您的wordpress数据库具有可靠的备份