It took me great time today to convert one site from https://example.com to https://sub.example2.com from WordPress multiste installation to normal WordPress installation (read: singlesite).
Since I started using wp-cli recently I thought it will be possible to do it only with wp-cli.
I used wp-cli to export a database. Since my instance was prefixed as: wp_9_
I used the code like this:
wp db export --tables="wp_users, wp_usermeta, wp_9_posts, wp_9_comments, wp_9_links, wp_9_options, wp_9_postmeta, wp_9_terms, wp_9_term_taxonomy, wp_9_term_relationships, wp_9_termmeta, wp_9_commentmeta"
While I later found I should use something like this:
wp db export --tables=wp_users,wp_usermeta, $(wp db tables --all-tables-with-prefix 'wp_9' --format=csv --allow-root) --allow-root
For those not using the #
wp db export --tables=wp_users,wp_usermeta, $(wp db tables --all-tables-with-prefix 'wp_9' --format=csv)
Then I set:
define('WP_HOME','https://sub.example2.com');
define('WP_SITEURL','https://sub.example2.com');
But I faced the problem: posts, guids, and meta not updated... Usually I never do the next step since it just works in numerous instances.
But I was forced to do the following:
UPDATE wp_posts SET post_content = replace(post_content, 'https://example.com', 'https://sub.example2.com');
UPDATE wp_posts SET guid = replace(guid, 'https://example.com', 'https://sub.example2.com');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'https://example.com', 'https://sub.example2.com');
Even further, I needed to update the uploads path, in the post content and even in the post_meta, since on mulitsite the uploads path was /uploads/sites/9/
while on a single site only /uploads/
.
This should also reflect in the posts and post meta like this:
UPDATE wp_posts SET post_content = replace(post_content, '/uploads/sites/9/', '/uploads/');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'/uploads/sites/9/', '/uploads/');
After this step, I manually copied files from /uploads/sites/9/ folder to the /uploads/ folder of the new WordPress instance on the same server using the cp
command. Sadly, I haven't even considered wp-cli for that.
I am asking is it possible to automate this processes entirely using wp-cli? It would be a great helper next time.