Replacing WordPress URLs with SQL command

WARNING – Always make a backup before modifying your database.

This came really handy when you have to manually migrate a WordPress website to another domain.

UPDATE wp_options SET option_value = replace(option_value, 'http://oldsite.url', 'http://newsite.url') 
WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'http://oldsite.url','http://newsite.url');

UPDATE wp_posts SET post_content = replace(post_content, 'http://oldsite.url', 'http://newsite.url');

UPDATE wp_postmeta SET meta_value = replace(meta_value, 'http://oldsite.url', 'http://newsite.url');

Remember that in some cases table prefix is not the standard “wp”, but it may change (and actually it is good practice), so remember to check that before running it.

Posted in Code Tricks, Transact SQL and tagged , , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.