Changing Wordpress URLs via MySQL or CLI

So when I pull down a Wordpress (PHP LEMP/LAMP) project to my local machine and need to quickly import the database from staging or production servers, these are the commands I run in MySQL. What it does is changes all references to the original site/home URLs in the database with a new one.

#  SQL
UPDATE wp_options SET option_value = replace(option_value, '', '') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, '', '');
UPDATE wp_posts SET post_content = replace(post_content, '', '');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'','');

# Yoast
UPDATE wp_yoast_indexable SET permalink = replace(permalink, '', '');
UPDATE wp_yoast_indexable SET twitter_image = replace(twitter_image, '', '');
UPDATE wp_yoast_indexable SET open_graph_image = replace(open_graph_image, '', '');
UPDATE wp_yoast_indexable SET open_graph_image_meta = replace(open_graph_image_meta, '', '');

If you just need a quick, nitty gritty way to get into your local database, what I typically do is just override it with something quick and easy. I do this to avoid having to hunt passwords locally.

# Local Dev
UPDATE `wp_users` SET `user_pass`= MD5('X') WHERE `user_login`='Y';

You can also use Wordpress CLI (wp-cli) to search and replace URLs as seen below:

wp search-replace '' ''

There’s always the wp-config.php override if all else fails for some very odd reason:

define( 'WP_HOME', '' );
define( 'WP_SITEURL', '' );

There’s also a functions.php option; I’m just gonna list it to make this article a bit longer, but I never use this:

update_option( 'siteurl', '' );
update_option( 'home', '' );