In case you ever need to find and replace some text in every post of your WordPress website, the following SQL code will help you out.
This could be necessary for a few reasons…
You might decide to move a website from staging into production.
Or you might decide to move a website to a new URL.
Most links in WordPress posts will be hard-coded as absolute URL’s for every image link and for every link to other articles within your blog.
So what do you do when you find yourself in a situation where you have hundreds of pages and URL’s all over the place pointing to the wrong address?
Execute the following SQL code against the MySQL WordPress database to swiftly update the entire website at once:
UPDATE wp_posts SET post_content = REPLACE(post_content, ‘www.old.com’, ‘www.new.com’);
This code will search through every “post_content” field in the wp_posts table and replace the “old” address with the “new” address.
The exact code could vary slightly depending on your table names. Here’s another way to visualize the same code:
UPDATE [your_table_name] SET [your_table_field] = REPLACE([your_table_field], ‘[string_to_find]’ , ‘[string_to_be_replaced]’);