WP 3.4 has missing photo data

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?

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'] . "https://wordpress.stackexchange.com/" . $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>';

Leave a Comment