Code to search and replace in a post in a specific category on wordpress ?

Discussion in 'Databases' started by z80039, Jul 28, 2010.

  1. #1
    Hi ,
    Any code to search and replace in all the posts of a specific category in wordpress ?

    thanks
     
    Last edited: Jul 28, 2010
    z80039, Jul 28, 2010 IP
  2. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Internally, wordpress splits this data up into four tables: wp_posts, wp_terms, wp_terms_relationships and wp_terms_taxonomy.
    So it can end up being a chunky query. This should do it... replace 'Text to replace', 'I have been replaced!' and 'YourCategory' with your own values.



    
    UPDATE wp_posts AS p
    JOIN wp_term_relationships AS r ON p.ID = r.object_ID
    JOIN wp_term_taxonomy AS t ON r.term_taxonomy_id = t.term_taxonomy_id
    JOIN wp_terms AS tt ON t.term_id = tt.term_id
    SET p.post_content = replace(p.post_content,'Text to replace','I have been replaced!') WHERE p.post_type='post' AND tt.name = 'YourCategory' 
    
    PHP:
     
    Deacalion, Jul 28, 2010 IP
    z80039 likes this.
  3. z80039

    z80039 Well-Known Member

    Messages:
    517
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    130
    #3
    Thank you very much,
    I will try it...
     
    z80039, Jul 29, 2010 IP