WP-CLI

Importing several XML Files to WordPress with WP-CLI

Learn how to import multiple XML files to WordPress automatically by using the command line.

I’ve been working at Automattic (the company behind WordPress.com and many others) for three years now and I’m still living the first statement of our creed:

I will never stop learning.

Today I learned something interesting about  WP-CLI and I believe it can help you to import a huge number of XML files to your WordPress automatically just the way it did to me.

Since November 2015, I work as a Happiness Engineer offering support to WordPress.com users but, since early 2018, I’m investing part of my days on a second level of support. 

The issue

During one of my shifts working on the second level support, I had to move around 320,000 posts (yup, 320 THOUSAND POSTS) from a site to another one, which is not something as easy as simply exporting database tables due to the relationship of posts created on multiple tables.

Fortunately, there is a tool on WordPress to export and import content by using XML files but I didn’t dare to import  6Gb of XML at once, so I split the file into several smaller ones. 300 files, 20Mb each.

Having hundreds of files to import created another issue

The WordPress native import tool handles only one XML at once.

The solution

I started doing some research to find a better and more efficienty way to proceed with the import process than importing each file manually on `Tools > Import` – that could take several days depending on my upload speed and other variables. So I decided to use WP-CLI.

I had used the WP-CLI command `import` to import individual files before but I got excited after reading the <file> description on  its documentation:

<file>…
Path to one or more valid WXR files for importing. Directories are also accepted.

Yes! It means the <file> option can also be used to import all XML files added to a directoryWe just need to enter a directory path instead of a file path!

How to use it

  1. WP-CLI must be installed on your environment (localhost or on your server).
  2. Create a directory with all the XML files to import on it.
  3. Run the following command on your terminalwp import /directory-path/ --authors=skip --skip=attachment.

Note that the options `–authors=skip` and `–skip=attachment` are optional and you should remove them depending on your import requeriments.

The full list of options supported by the wp import command is available on the official WP-CLI documentation

2 Comments

    • Hi there!

      Thanks for taking some time to comment here 🙂

      I think publishing a new post about it won’t help that much because the only steps I can think of are A. Installing WP-CLI on your environment. B. Creating the folder with the XML files.

      The steps to install WP-CLI are described on their own site:

      https://wp-cli.org/#installing

      If you are trying to import files to your local environment, it is possible to create the folder and add all XML files to it by using the file manager installed on your computer (it depends on your SO, though).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: