Updating URL’s in the database after WordPress migration
I’m putting this here as it’s a code snippet I use all the time and if anything, it makes it easier for me to find.
Once you’ve migrated a WordPress site from one hosting to another (or set up a site locally) you will likely want to update all the URL’s throughout your website so they point to the correct place.
(I use phpmyadmin so I will talk about it in that context.)
Once you’ve imported your database into phpmyadmin, you’ll want to navigate to the SQL tab (see image below)
Then you’ll want to paste in the following code:
UPDATE wp_options SET option_value = replace(option_value, 'http://oldurl.com', 'http://newurl.com') WHERE option_name = 'home' OR option_name = 'siteurl'; UPDATE wp_posts SET guid = replace(guid, 'http://oldurl.com','http://newurl.com'); UPDATE wp_posts SET post_content = replace(post_content, 'http://oldurl.com', 'http://newurl.com'); UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://oldurl.com','http://newurl.com');
You’ll need to replace the http://www.oldurl.com and http://newurl.com with your own URLs (don’t include trailing slashes).
Then press ‘Go’ and it should let you know how many rows it has replaced the URL in.