How to migrate blogs between WordPress sites using WP-CLI

For several reasons, the need to migrate blog posts from one site to another may arise. Maybe you changed your domain as part of your marketing strategy or you have several sites and want to centralize the blog for all of them. Whatever the reason may be, the procedure is simple.

First of all, we will require ssh access to the server and wp-cli installed.

Why use wp-cli instead of the admin interface? When migrating large sites, issues with uploading big files and procedures that take too long arise if we use the the wordpress administration. Instead of tweaking our server to be able to migrate it that way, we just do it using tools more appropriate for the task at hand.

There are 4 steps to this:

  1. Export posts from the source site using wp export. This can be achieved with the following command:
    wp export --post_type=post
    You can use the --dir argument if you want the export file in a specific path.
  2. This step is optional, but recommended. Take a backup from your site, specially the database. You can do that by using the wp db command:
    wp db export latest.sql
  3. Move posts from source to target. This requires running the same command twice. The first time will create a file with the mapping for your users in both sites, which you have to edit. After it is edited, you run it again and it will map the users while migrating the posts according to that file. The format of the file is pretty simple, just asks who should be the author for every author in your old site:
    wp import "file-exported-from-source.xml" --authors=mapping.csv
  4. Add the redirects from source to target site. If your old site has traffic, you don’t want to lose it, so you just redirect them to your content’s new home. Most of the time a simple redirect rule to your .htaccess will suffice, but if you have custom structures more work may needed for this. On the simplest case the following will be enough:
    RedirectMatch 301 ^/<source_blog_url>/(.*)/<target_blog_url>/$1/

Review that your site is working as expected and that your migrated posts are there, including their images. That’s it!

Do you need some help?

Let's get started. Tell us a little about yourself and your organization
and we can start a conversation about your needs and our capabilities.

Related Posts