MySQL REPLACE. Replacing content in WordPress

May 19th, 2010 by Alex Leave a reply »

Sometimes, when you make some changes, like new domain url or fixing tags (for ex. replacing <br> to <br />) you need to run a simple query and not do crazy PHP magic. Here is the quick example of how you use the MySQL REPLACE function.

To replace old url with new url in WordPress posts content:

UPDATE wp_posts SET post_content = REPLACE(post_content, "http://oldurl.com", "http://newurl.com")

To replace all <code> tags with <pre> run these 2 queries. Note that i put <code, that’s in case there are some attributes in the tag

UPDATE wp_posts SET post_content = REPLACE(post_content, "<code", "<pre")
UPDATE wp_posts SET post_content = REPLACE(post_content, "</code", "</pre")

Comments are closed.