Last Updated: February 25, 2016
·
1.255K
· jamart

Migrating Wordpress databases

UPDATE wp_posts SET guid = REPLACE (guid, 'http://oldsite', 'http://newsite');
UPDATE wp_posts SET post_content = REPLACE (post_content, 'http://oldsite', 'http://newsite');
UPDATE wp_postmeta SET meta_value = REPLACE (meta_value, 'http://oldsite', 'http://newsite');
UPDATE wp_options SET option_value = REPLACE(option_value, 'http://oldsite', 'http://newsite');

If you want change the wordpress tables prefix, you need make the next query:

UPDATE new_options SET option_name = 'new_user_roles' WHERE option_name = 'old_user_roles';
UPDATE new_usermeta SET meta_key = 'new_capabilities' WHERE meta_key = 'old_capabilities';

1 Response
Add your response

At first glance a good idea, but if you take a look into the database you will notice that some data is php-serialized.

php-serialized means, that an array was converted into a string (in this case to be stored in a database) and it looks like:

a:4:{s:25:"dashboard_recent_comments";a:1:{s:5:"items";i:5;}s:24:"dashboard_incoming_links";a:5:{s:4:"home";s:54:"http://testsite.preview.ogilxxxxxx.de";s:4:"link";s:130:....

This structure is always built with the information about the string length. In this case: 54 chars. If your new Url has another length, the database field can't be unserialized. To replace the URL you have to unserialize the string, replace the url in the array and serialize it back.

Wordpress recommends the search and replace script. [1]

In addition I think the system of storing absolute urls in the database might not be a good idea, but the wordpress team made this decision unfortunately.

over 1 year ago ·