Search And Replace In WordPress Post

Posted on November 2006 at about 12:29 AM by hwa

Upon trying to achieve XHTML 1.0 Strict, one of the thing i found out that i had been doing wrong all this while is that i put target=”_blank” HTML tag in most of my post link, this is not a valid XHTML for Strict and it won’t pass the validation, but i had been doing this since the very first day i blog! It will be troublesome if i’m going to search for every post and take away the target=”_blank”!! So i guess the only way to do this easily is by using the MySQL query, i went to Google around and found this very useful SQL query in Lorelle’s blog.

Before proceed to make any changes to your WordPress database, it’s always a good idea that you backup your database first, who know you might missed out something and screw up your database, it’s always better to prevent than cure right? 😉

Continue with the search and replace, in order to do search and replace in WordPress database, you can use the REPLACE syntax in SQL query, the full line would like the following:

  1. UPDATE wp_posts SET post_content = REPLACE (
  2. post_content,
  3. 'Item to replace here',
  4. 'Replacement text here');

Where wp_posts is your WordPress database post table and post_content is your post content table field, most of the time you don’t need to change this two value.

The thing you need to change is obviously the Item to replace here, which is the text you want to search, it can be multiple string as long as the string is within the single quote, and Replacement text here is the text you want to replace, also can be multiple string as long as it’s within the single quote.

So in my case, i want to strip away target=”_blank” and replace it with nothing, so i do this:

  1. UPDATE wp_posts SET post_content = REPLACE (
  2. post_content,
  3. ' target="_blank"',
  4. '');

Note that the i didn’t put anything for replace string, because i want to delete target=”_blank”, it’s a two single quote.

Again i would like to say, it’s a good practice that you backup your database always, once you screw up your database there is no turning back, and i will take no responsibility if something happen to your database, so do this at your own risk! 🙂

5 Responses to “Search And Replace In WordPress Post”

  1. manu khanna Says:

    Dude u r a life saver man, I ws just abt to open each and every post that had target=_blank in it and change it to something else. thnx so much, everythign seems to be working.

    Hey can you let me know how u include that “post Summary” thingy after Related Topics on each post page? The one that says

    ” This entry was posted on Monday, November 13th, 2006 at 12:29 am and is filed under WordPress. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.”

  2. hwa Says:

    good to hear that my post is actually helped someone 🙂

    regarding the post summary, u can find it in the default WordPress theme, just go to single.php and all the thing are within the div class “postmetadata alt”.

  3. manu khanna Says:

    dude my knowledge abt coding is zilch, would reallu appreciate any help. i really don’t know what I need to remove from the index.php file and where on earth is “the Loop”

  4. hwa Says:

    hmm.. it’s kinda long for me to paste the code here, but it’s not that hard to add it into your theme, the post summary is just inside the single.php of your default WordPress theme.

    go to your WordPress folder, then wp-content > themes > default, open up single.php and scroll down a bit, you should be able to see something like “This entry was posted…” within the <small> html tag, that’s the post summary part, copy all the thing inside <small> and </small> and paste it at where you want the post summary to appear in your blog, most probably at your single.php inside your WordPress theme folder.

    hope this helps…

  5. JoLynn Braley Says:

    Hi, thanks a lot, this was just what I needed to remove “[tag]” and “[/tag]” from over 300 posts after a plugin stopped working.

    Thanks again!

Leave a Reply

You must be logged in to post a comment.