We create beautiful & user-friendly websites and provide quality web development services worldwide.
Contact us to get started on your project.


How to Quickly Find and Replace Multiple Instances of Text In WordPress

Published by Veron Ang | Leave a comment

textI run and maintain multiple WordPress sites for clients, and there have been many a time that I need to replace certain recurring texts in the posts. This may be a word, a phrase or even lines of HTML code that had been entered into the post content and they need to be changed or removed.

For instance, a frequently mentioned company name or website URL has changed and you need to update your posts to reflect the new name or URL. For most people, the obvious thing to do would be to log in to WordPress admin, and manually edit the posts one by one. But what if you have dozens of posts to edit? Thankfully there’s a much quicker way to do this that will only take seconds. The answer to this is the REPLACE() function in mySQL.

In phpMyAdmin (or whichever SQL client you use), run the SQL query:

UPDATE table_name SET field_name = REPLACE(field_name, 'Old string', 'New string');

For instance, if I have recently migrated a site from olddomain.com to newdomain.com (as mentioned in a previous tutorial), there will be several times that olddomain.com was mentioned in the posts that would all need to be changed to newdomain.com. Here’s the SQL query to use:

UPDATE wp_posts SET post_content = REPLACE(post_content, 'olddomain.com', 'newdomain.com');

This can also be done to remove texts, by simply making the second parameter an empty string, like this:

UPDATE table_name SET field_name = REPLACE(field_name, 'Text to remove', '');

Few things to note:

  • The strings are case-sensitive.
  • This works for multiple lines too!

The REPLACE() function is a very powerful one. Hence always ALWAYS remember to backup your database before making any such changes.

Tags: ,

About the Author

Veron Ang is a , developer and WordPress expert at Sparklette Studio. She graduated with a bachelor's degree in computer science from the National University of Singapore. Engage her to work on your next project by sending her a message from the contact page or on Twitter.

Leave a comment