How to Move WordPress Media Library to a New Site

In this tutorial, I am going to discuss different ways of moving the WordPress media library to a new site – both for multisite installations and standalone WordPress sites.

Moving Media Library within a WordPress Multisite Network

Let’s start with the multisite installation first.

Maybe you don’t even need to move it, actually?

First of all, I’d like to remind you that media files of all sites in a WordPress multisite network are sharing the same uploads folder, they are just separated by sub-folders, like /sites/2, /sites/3, etc, which basically represents the ID of a site in a network.

So, for example, if your goal is to use media files of “Site 2” on “Site 3”, then why to copy the whole folder /sites/2 (which can be huge by the way) into /sites/3 instead of using them directly from the original site folder /sites/2?

Sounds interesting? But how to do it?

It requires just two simple steps:

  • First – you need to install and network activate my Multisite Shared Media Library plugin.
  • Second – decide which site’s media library you want to move, and instead of moving it, configure it as a shared media library using the WordPress filter hook below:
add_filter( 'rudr_sml_network_library_id', function( $network_library_id ) {
	// for example, in our case it is Site 2 (with ID = 2)
	return 2;
} );

That’s pretty much it. Now you can select media images from this specific site from the “Shared Media” tab:

Using shared media library instead of moving it
In other words, why duplicate the media library of the whole sub-site when you can just access it in another tab?

Duplicating the whole site

Let’s take a look at the following example situation: You have just created a new site in your WordPress multisite installation and now you want to move a media library there. But instead of following all the steps in this tutorial, you could just duplicate this specific subsite.

If yes, in this specific case, everything you need to do is install and network activate my Duplicate Site for WordPress Multisite plugin, then go to Sites > All Sites and hit the “Duplicate” link.

WordPress Multisite clone site example
You can clone any sub-site of your WordPress Multisite network in one click.

Done!

Move media files and database tables to a new site

If your goal is specifically to move media from one of the sub-sites of your WordPress multisite network to any other one, then you can follow the steps below.

Let’s start with the media files. You need to go to the uploads/sites folder and find a media folder dedicated to a target site. For example, if we’re moving our media library from “Site 2” to “Site 3”, it means that we can simply rename the folder /sites/2 to /sites/3, or, in case “Site 3” already has some media files that we don’t want to remove, then you just need to merge these two folders.

Once you’ve done that, your media files will not magically appear in your WordPress admin of “Site 3” because, of course, we need to move the database entries as well.

Because we’re currently talking about a WordPress Multisite installation, it will be just enough to run a bunch of SQL queries.

First, let’s copy attachments from wp_2_posts to wp_3_posts:

INSERT `wp_3_posts` SELECT * FROM `wp_2_posts` WHERE post_type = 'attachment'

Second, we need to think about guid. It is not that necessary, and most likely, everything will work out pretty well even if guid points to the previous site, but sometimes it can be used in attachment-related WordPress functions, for example, in wp_get_attachment_url(). So, let’s run the search-and-replace query:

UPDATE `wp_3_posts` SET guid = replace( guid, 'DOMAIN/wp-content/uploads/sites/2/','DOMAIN/wp-content/uploads/sites/3/' )

Third, let’s talk about the attachment metadata. The cool thing about it is that, basically, we need only the following three meta keys:

  • _wp_attached_file,
  • _wp_attachment_image_alt,
  • _wp_attachment_metadata.

So, we can easily select and copy them all using the following SQL query:

INSERT `wp_3_postmeta` SELECT * FROM `wp_2_postmeta` WHERE meta_key LIKE '%wp_attach%'

No replacements are needed in the attachment metadata, which is totally great!

Oh yes, if you want to remove media files from the original sub-site, the following SQL queries can help you with that:

DELETE FROM `wp_2_posts` WHERE post_type = 'attachment';
DELETE FROM `wp_2_postmeta` WHERE meta_key LIKE '%wp_attach%';

Moving Media Library to a New Standalone WordPress Site

I am going to show you two methods here. The first one will work for pretty much any WordPress website, though it may not be convenient if you have really large media libraries. The second one – will work only when you’re moving the media library to a new website.

Using Import / Export tools

When moving a media library to another WordPress site which is not part of a Multisite network, we can easily use the WordPress build-in export tool in Tools > Export.

WordPress move media library to a new site
Using the WordPress export tool to move the media library to a new site.

On the target site, accordingly, you could use the WordPress import tool. It will probably ask you to install and activate the “WordPress Importer” plugin.

Move the media library to another WordPress site
Do not forget about the “Download and import file attachments” checkbox; otherwise, be prepared for an error: “Fetching attachments is not enabled.”

Manually copying files via FTP and exporting database

This method is similar to the one we used before for a WordPress multisite network, but please keep in mind that it will work great only when you’re moving the media library to a new website.

So, first of all, we need to copy the media files from one site to another via FTP.

Once you’ve done that, please open the database of the first website, for example, in phpMyAdmin, and run the following SQL query there:

SELECT * FROM `wp_posts` where post_type = "attachment"

Of course, you need to replace wp_ with the database prefix you’re actually using on your website, in case you changed it before,

Once the query results are displayed, scroll down a little bit and click the “Export” button:

export database query results in phpMyAdmin

Switch from the “Quick” to the “Custom” export method, and then you need to export only data without the table structure:

phpMyAdmin export data only

Done.

The next thing you need to do is a couple of replacements in the exported SQL file.

  • If the sites have different database prefixes, you need to replace the old prefix with the new one inside the SQL file before doing the import.
  • Replace an old domain with a new one, or you can just run the guid replacement SQL query UPDATE wp_posts SET guid = replace... for a new site, which we have already discussed before.

Now let’s proceed to the metadata; we need to run another SQL query in the “Site 1” database:

SELECT * FROM `wp_postmeta` where meta_key LIKE "%wp_attach%"

No replacements are needed in the metadata export file, only change the database prefix if your sites have different ones.

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on X