Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I just updated to WP3.4 and discovered that previously available images are no longer "found" by the system. By this I mean that all the image files remain online and accessible, but WP has lost thumbnail data and subsequently shows the standard media icon on the media library list. Front-facing pages that should have images are now displaying the generic "image missing" icon because there is no URL (I checked the source).

My guess is that somehow the connection between database and image listings have broken, and I"m trying to reverse engineer them to find differences. Hopefully there are others with a similar problem and have worked through it?

share|improve this question

1 Answer 1

Having figured out the issue, I'd like to update this question. The issue stems from previous versions of WP not including the _wp_attached_file meta key when uploading media, which 3.4 now seems to require.

Below is PHP code for looping through the database, verifying the presence of both the key and image file, then updating the database were possible.

// descend through the database
$updated = 0;
$skipped = 0;
$error = 0;
$upload_dir = wp_upload_dir();

$sql = sprintf("select * from %s where post_type = 'attachment'", $wpdb->posts);
$all_attachments = $wpdb->get_results($sql);

foreach ($all_attachments as $attachment) {
    // get the meta value
    $meta = get_post_meta($attachment->ID, "_wp_attachment_metadata", true);
    $file = $meta['file'];

    // verify that the file exists
    $file_path = $upload_dir['basedir'] . '/' . $file;
    if (!file_exists($file_path)) {
        $error++;
    }
    else {
        // add the meta value, which returns false if it already exists
        $adding_meta = add_post_meta($attachment->ID, '_wp_attached_file', $file, true);
        if ($adding_meta)
            $updated++;
        else
            $skipped++;
    }
}
echo '<div id="message" class="updated"><p>' . sprintf("%d attachments were updated, %d were skipped and %d had errors.", $updated, $skipped, $error) . '</p></div>';
share|improve this answer
    
Where does this code go? I have been searching all day for this, I think. –  user18506 Jul 25 '12 at 1:19
    
This could can go anywhere in the admin panel of your theme/plugin that can be executed. Wrap it in a $_GET variable and use a link, just to run it once. –  MechEngineer Jul 25 '12 at 1:27
1  
For those like Andrew who have a similar problem and would like it in a more user-friendly format, I've wrapped my above code in a super basic plugin, available at wesg.ca/dx8. –  MechEngineer Jul 26 '12 at 1:40

protected by Community Sep 1 '12 at 4:17

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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