WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How to search/replace post content based on Category

[Resolved] How to search/replace post content based on Category

  • I’m trying to do this via MySQL, but would love to do it with a plugin.

    I want to search/replace (and ideally concat) post content, but only posts that belong in one category.

    I can do this which shows all the posts in the category 7266, which is a good:

    SELECT DISTINCT ID, post_title, post_name, guid, post_date, post_content
    FROM wp_posts AS p
    INNER JOIN wp_term_relationships AS tr ON (
    p.ID = tr.object_id
    
    )
    INNER JOIN wp_term_taxonomy AS tt ON (
    tr.term_taxonomy_id = tt.term_taxonomy_id
    AND taxonomy = 'category' AND tt.term_id
    IN ( 7266 )
    )
    ORDER BY id DESC

    And I can do this which will add a tagline to all posts:
    UPDATE wp_posts SET post_content = CONCAT(post_content, 'This is the category tagline')

    But how do I combine those two to only add the tagline to posts in category 7266?

    PS: I know I can add taglines based on category through PHP, but for a variety of reasons that won’t work for what I need. I need to actually edit the content in the database based on category.

Viewing 3 replies - 1 through 3 (of 3 total)
  • I think this will do the job. Be sure to BACK UP first!!

    UPDATE wp_posts p
    INNER JOIN wp_term_relationships AS tr ON (p.ID = tr.object_id)
    INNER JOIN wp_term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id
    AND tt.taxonomy = 'category' AND tt.term_id
    IN ( 7266 )
    )
    SET p.post_content = CONCAT(p.post_content,' xxxxxxxxxx')

    This is working!!!

    Thank you so much vtyzzy! I searched all over the place for this, but couldn’t find an example anywhere. I’ll be sure to share the info.

    Please use the dropdown on the right to mark this topic ‘Resolved’ so that others researching the same question can see that there is a solution.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] How to search/replace post content based on Category’ is closed to new replies.
Skip to toolbar