Skip to content

Instantly share code, notes, and snippets.

@jamiehs
Last active September 24, 2015 17:12
Show Gist options
  • Save jamiehs/9647075 to your computer and use it in GitHub Desktop.
Save jamiehs/9647075 to your computer and use it in GitHub Desktop.
MySQL Find and replace for WordPress domain changing
# Table Prefix
SET @prefix = 'wp';
# Find and replace strings
SET @find = "www.example.com";
SET @replace = "www.example.dev";
# Build and execute the statements
SET @sql1 = CONCAT('UPDATE ', @prefix, '_options SET option_value = REPLACE(option_value,?,?)');
SET @sql2 = CONCAT('UPDATE ', @prefix, '_posts SET guid = REPLACE(guid,?,?)');
PREPARE stmt1 FROM @sql1;
PREPARE stmt2 FROM @sql2;
EXECUTE stmt1 USING @find, @replace;
EXECUTE stmt2 USING @find, @replace;
DEALLOCATE PREPARE stmt1;
DEALLOCATE PREPARE stmt2;
# Update wp_options table
UPDATE wp_options SET option_value = REPLACE(option_value,'www.example.dev','www.example.com');
# Update wp_posts table
UPDATE wp_posts SET guid = REPLACE(guid,'www.example.dev','www.example.com');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment