Having recently been the victim of WordPress site hack (vandalized) I was forced to move to a new hosting company, and move over all my content, but I didn’t want to copy my database verbatim, as I was worried that there may have been some SQL (Iframe like injected) into portions of my database. So what I did instead was install a fresh copy of WordPress from a known clean source, carefully installed a few critical plugins (again highly used plugins known to be clean) , then slowly began moving over my database mainly the wp_posts , wp_users etc. And periodcially running internal scans (WordFence plugin is great for this ) and external site checkers, to make sure I didn’t re-introduce any compromised code or images. By the way here’s a great all-inclusive article on WordPress security ultimate guide
Media Gallery – empty (only showed placeholders)
So after I completely uploaded all my images (and their re-sized relatives) , I went into WordPress’s dashboard Media Gallery to verify they were all there. The names all appeared but with an empty (blank placeholder) instead of the appropriate thumbnail. This also caused issues on the home page because the theme I was using used a featured image for each positng and it too was blank.
So after poking around a bit on the web , I realized this is a pretty common experience, for self-hosted WordPress sites where the images are manually uploaded (ftp’d) to the wp-content/upload folder. Because going outside of WordPress Media Gallery upload process none of the corresponding meta data is saved in the database telling word press about the images, and their WordPress Gallery state.
The good news is the images are all there , and they appeared fine in the postings, it was only when I went to the gallery to view that they were “missing”.
How to Re-Attach Media Images
So after poking around a bit I discovered that the WordPress database table postmeta contains all sorts of meta data (see WordPress Database Diagram for greater explanation of tables) for posting. So I looked at my backed-up database SQL from my hacked site, and noticed all my images had entries in this postmeta table, so I issued the command below to copy all the attached files (image) and metadata (mostly JSON) to the new table. I could have just copied the old postmeta table over the new one, but again I wanted to avoid bring over any infected rows.
So use something like your web host’s PhpMyAdmin (typically found in cPanel) and issue a command like the one below, you may get conflicts because even an empty postmeta table will have about 200 entires in that table, so that’s what the meta_id > 200 does. Of course you may need to go back and manually re-associate the conflicting media id’s but they should only be a few.
INSERT INTO live_db.wp_postmeta SELECT * FROM backup_db.wp_postmeta WHERE ( meta_key="_wp_attached_file" OR meta_key="_wp_attachment_metadata" ) AND meta_id > 200
After issuing that query you should be able to go into you’re gallery and see the images restored.