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, 'https://search.com', 'https://example.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'https://search.com', 'https://example.com');
UPDATE wp_posts SET post_content = replace(post_content, 'https://search.com', 'https://example.com');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'https://search.com','https://example.com');

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

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:

# WPCLI
wp search-replace 'https://search.com' 'https://example.com'

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

define( 'WP_HOME', 'https://example.com' );
define( 'WP_SITEURL', 'https://example.com' );

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', 'https://example.com' );
update_option( 'home', 'https://example.com' );
ender