NOTE: An absolute URL is the fully qualified path to a resource; such as http://www.barrywise.com/images/image.jpg. A relative URL will only work when the file is relative to the path of the current page, such as ../images/image.jpg.So here I was with a couple hundred pages of content with image URLs all pointing to the wrong address on the staging server. I needed a quick way to do a find and replace on every post in the WordPress MySQL database. It was actually pretty simple. In my case, I just had to execute this SQL code against the MySQL WordPress database:
UPDATE wp_posts SET post_content = REPLACE(post_content, 'staging.server.com', 'www.productionserver.com');
This code will search through every “post_content” field in the wp_posts table and replace the staging URL address (staging.server.com) with the production address (www.productionserver.com). The format is easy to understand and can of course be used with any MySQL table, not just WordPress:
UPDATE [your_table_name] SET [your_table_field] = REPLACE([your_table_field], '[string_to_find]' , '[string_to_be_replaced]');
I realize this isn’t the most complex or enlightening code sample, but I hope this helps anyone stuck with an annoying repetitive text string in all your WordPress posts which you’d like to search and replace with one quick command.
Search and Replace in MySQL database with phpMyAdminDo a search and replace in any MySQL database in phpMyAdmin. You do not need to download the database export at all, just run this simple command. This also works through the MySQL command line.
To search and replace a text string, start up phpMyAdmin, and click on your database name that you want to run the search and replace through. At the top of the window, click on the "SQL" tab.
In the text box, enter the following code. This is the generic setup, so edit to satisfy your needs:
UPDATE tablename SET tablefield = replace(tablefield,"findstring","replacestring");
You can add a WHERE clause onto this as well.
For example, here is one command a ran:
UPDATE `mos2_content` SET introtext = replace(introtext,"<p> ","") WHERE `title` REGEXP '-0';
This got rid of all paragraph tags in the mos2_content table where the title included the string "-0".
Hope this helps. If you have any comments, suggestions, questions, or your own code, please submit a comment below.