How To Fix Auto Post Thumbnail on WordPress 3.4 (SEO-Friendly!)

Facebook Twitter LinkedIn
Back to blog

Have your thumbnails gone missing since upgrading to WordPress 3.4? Are you using the Auto Post Thumbnail plugin? Turns out you’re not alone.

The problem is down to the new way WordPress stores additional data (metadata) associated with a post - in this case the thumbnail WordPress should display on index pages etc.

Existing hotfixes

A hotfix has appeared but it only fixes the issue when you add a post – it does not fix existing images. Another hotfix involves deleting all broken images and running the plugin's Generate Thumbnails tool, but this has a fundamental flaw in SEO terms: your existing image URLs will no longer be discoverable and your pictures will soon enough drop out of Google Images. Bye bye traffic...

PHP to the Rescue

Retaining your existing image URLs is simply a matter of re-establishing the link in the database between your post and its long-lost thumbnail. To do this, copy the following code into a PHP file (e.g. imagefix.php ) and upload this to your webroot. Then fire up a browser and visit the page you just uploaded e.g. http://www.example.com/imagefix.php.

The script should take less than a minute to do its thing and voilà: your templates and galleries will look just the way you remember - and Google will never know what all the fuss was about!

Note: as well as running the script you should install the hotfix here upgrade the plugin, so that future posts continue to auto-add the thumbnail.

DISCLAIMER: Use this code at your own risk. It is largely untested and ClickThrough Marketing take no responsibility for any issues that may arise due to its use. TAKE A BACKUP OF YOUR DATABASE BEFORE RUNNING THIS SCRIPT.

<?php
ob_implicit_flush(1);
require_once("wp-load.php");
echo 'Working... '; ob_flush();
$sql = 'SELECT meta_value FROM `'.$wpdb->postmeta.'` WHERE meta_key="_thumbnail_id" ORDER BY post_id DESC';
$posts = $wpdb->get_results($sql);
foreach ( $posts as $post ){
    $sql = 'SELECT meta_id FROM `'.$wpdb->postmeta.'` WHERE post_id = '.$post->meta_value.' AND meta_key = "_wp_attached_file"';
    $check = $wpdb->get_results($sql);
    if(count($check)) continue; // We've already done this one...
    $sql = 'SELECT meta_key,meta_value FROM `'.$wpdb->postmeta.'` WHERE post_id = '.$post->meta_value.' AND meta_key = "_wp_attachment_metadata"';
    $thumb = $wpdb->get_results($sql);
    $data = unserialize($thumb[0]->meta_value);
    if(!isset($data['sizes']['thumbnail']['file'])) continue;
    $date = dirname($data['file']);
    $filename = $data['sizes']['thumbnail']['file'];
    $sql = 'INSERT INTO `'.$wpdb->postmeta.'` SET post_id='.$post->meta_value.', meta_key="_wp_attached_file", meta_value="'.$date.'/'.$filename.'"';
    $wpdb->query($sql);
}
echo 'done!';
?>

Talk To Us About Accelerating your Digital Performance