50

I'm exporting a Wordpress site from localhost to a web host, and I am unable to import the Media Library, as the web host is unable to contact localhost.

I've uploaded all of the localhost files from /wp-content/uploads/..., and I'm thinking I just need to isolate the part of the MySQL database which contains the Media Library, and adjust the URL, then import the SQL into the web host database.

Can you tell me where the Media Library lives in the MySQL database please?

3 Answers 3

42
Select * from wp_posts where post_type = 'attachment';

Will return all the entries in the Media Library.
After the execution, you can export the result table as SQL, or CSV, or any other portable data format you like. Remember, if you are not sure if the entries already exist in your database, use the INSERT IGNORE statement instead of INSERT. (This is possible through the export panel in phpMyAdmin or other MySQL clients).
Also, there are entries referring to the Media Library in each post, such as attachment images or thumbnail images, which are stored in the wp_postmeta table. Wordpress stores them so the media "attaches" to posts or pages. If you want those to be exported too, you will need to use something like this :

 SELECT * FROM  `wp_postmeta` 
 WHERE meta_key IN (
   '_wp_attached_file', 
   '_wp_attachment_backup_sizes',  
   '_wp_attachment_metadata',  
   '_thumbnail_id'
 )

And then you can export them to wherever you want. It is all I know about media library stuff in Wordpress.

3
  • 10
    You've provided me a small bit of light in the world of darkness that is WordPress development
    – kbuilds
    Oct 12, 2017 at 23:47
  • What is "exporting pan" ? Jul 21, 2020 at 2:08
  • 1
    @SherylHohman I think that they meant "exporting panel". That's what I get from the context... Feb 5 at 12:39
39

The Media Library lives in both wp_posts and wp_postmeta.

  • wp_postmeta contains the image URL
  • wp_posts contains an entry for each image insertion into a post, along with the post ID.

Exporting and importing these 2 tables as SQL did not work for me - I received 'duplicate entry for key 7'...

Exporting and importing these 2 tables as CSV did work, using "CSV using load data".

Before importing, I emptied the 2 tables in the recipient database.

1
  • 1
    An alternative way to move from local developer to live remote host is to use WP Migrate DB.
    – Steve
    Feb 3, 2017 at 8:16
2
SELECT    
    p.ID,
    p.post_title AS title, 
    p.post_name AS post_name,
   (SELECT meta_value from wp_postmeta where post_id = m.meta_value and meta_key='_wp_attachment_metadata') AS meta_value
FROM 
    wp_posts p, 
    wp_postmeta m
WHERE
        p.post_type = 'post'
    AND p.post_status = 'publish'
    AND p.id = m.post_id
    AND m.meta_key = '_thumbnail_id'
ORDER BY 
    p.post_date DESC
LIMIT 5;

And then use PHP unserialize 'meta_value'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.