Skip to content

Instantly share code, notes, and snippets.

@doitlikejustin
Created July 26, 2013 20:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doitlikejustin/6091841 to your computer and use it in GitHub Desktop.
Save doitlikejustin/6091841 to your computer and use it in GitHub Desktop.
WordPress SQL replace old URL with new URL
SET @prefix = "wp_";
SET @old = "http://www.old.com";
SET @new = "http://www.new.com";
SET @sql1 = CONCAT('UPDATE ', @prefix, 'options SET option_value = REPLACE(option_value,?,?)');
SET @sql2 = CONCAT('UPDATE ', @prefix, 'posts SET guid = REPLACE(guid,?,?)');
SET @sql3 = CONCAT('UPDATE ', @prefix, 'posts SET post_content = REPLACE(post_content,?,?)');
PREPARE update1 FROM @sql1;
PREPARE update2 FROM @sql2;
PREPARE update3 FROM @sql3;
EXECUTE update1 USING @old, @new;
EXECUTE update2 USING @old, @new;
EXECUTE update3 USING @old, @new;
DEALLOCATE PREPARE update1;
DEALLOCATE PREPARE update2;
DEALLOCATE PREPARE update3;
SET @old = "http://www.old.com";
SET @new = "http://www.new.com";
UPDATE wp_options
SET option_value = replace(option_value, @old, @new)
WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts
SET guid = replace(guid, @old, @new);
UPDATE wp_posts
SET post_content = replace(post_content, @old, @new);
@130db
Copy link

130db commented Jul 26, 2013

Thanks, man!
This is one useful piece of SQL :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment