WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to search/replace post content based on Category (4 posts)

  1. kevine
    Member
    Posted 3 years ago #

    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.

  2. vtxyzzy
    Member
    Posted 3 years ago #

    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')
  3. kevine
    Member
    Posted 3 years ago #

    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.

  4. vtxyzzy
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic