How to move all images from one WordPress website to another using WP-CLI? Print

  • WordPress, WP-CLI
  • 0

What is WP-CLI?

WP-CLI is a command line tool for managing WordPress websites which we have installed in all of our cPanel hosting accounts. It can be accessed either via SSH or via the built in Terminal within your cPanel account

What are the benefits of WP-CLI?

Although a lot of functionality is available in WordPress itself, WP-CLI adds functionality, flexibility and more tools to be able to interact with your WordPress website

Additionally as WP-CLI is run on the command-line it isn't constrained to the PHP execution time limits imposed some hosting companies

How to run the export within WordPress?

If you are wanting to move all images from one WordPress website to another, you can use the tools available in the Dashboard in WordPress: Tools => Export Then select just "Media"

This will give you a XML file in a specific WordPress format that you are able to import to the new site

What is the command to move all images from one WordPress website to another using WP-CLI?

If you have access to the command-line, you can use the tools available in WP-CLI to create the same export file

To confirm the post-type that we want to export we can use the command wp post-type list which will give us a formatted table showing the currently confirmed post-types on the WordPress website

Output of wp-cli command 'wp post-type list' command.png

As you can see we have various post-types available, but the one that we are interested in is attachment under the name column.

Now that we know this information, we can use the command wp export with the optional --post_type= filter to be able to get just the data we want.

When we run the full command wp export --post_type=attachment we receive the output confirmation in our Terminal:

  Starting export process...
  Writing to file /home/[USER ACCOUNT]/[DIRECTORY]/[WEBSITE NAME].wordpress.2022-08-24.000.xml
  Success: All done with export.

Where the user account, directory and Website Name will be personalised to your specific environment

Importing to the new WordPress website

To be able to import the images to the new site, we again have the two options of using either the WordPress dashboard or WP-CLI

Using the Dashboard

Navigating to Tools => Import you will see a list of different platforms that you can easily import content. If you don't currently have the WordPress importer installed you will be prompted to "Install Now"; This will download and activate the plugin automatically, then give you the option to run the importer

WordPress importer Tools

During the import, after you have uploaded the export file and if the existing user from the previous site doesn't exist on the new site, you will be asked if you would like to create the user or allocate to an existing user

Using WP-CLI

With the previously exported file uploaded, you will be able to simply import the content using the command wp import to the new site

This needs a couple of additional parameters to complete successfully. Firstly you will need to specify the location of the import file, then you will need to tell the script how you would like the authors to be mapped

When the script has successfully run you will be greeted with the message "All done. Have fun! Remember to update the passwords and roles of imported users."

Common Errors

If you don't add the "author" parameter, you are presented with the following error message:

Error: Parameter errors: missing --authors parameter (How the author mapping should be handled. Options are 'create', 'mapping.csv', or 'skip'. The first will create any non-existent users from the WXR file. The second will read author mapping associations from a CSV, or create a CSV for editing if the file path doesn't exist. The CSV requires two columns, and a header row like "old_user_login,new_user_login". The last option will skip any author mapping.)

If you don't have the WordPress Importer plugin installed you will receive the following message prompting you to install it:

Error: WordPress Importer needs to be installed. Try 'wp plugin install wordpress-importer --activate'.

Was this answer helpful?

« Back

Find your perfect domain

Use our domain name search to check your dream domain name

Wide Variety of Domains

Easy Registration Process

Free WHOIS Privacy