For some reason, some of the thumbnails of my media library are randomly becoming missing.
I’ve found out _wp_attachment_metadata
isn’t present in the database for those images with missing thumbnails. The files, however, are present on disk – both the original file and the “cropped” sizes.
The purpose of this question is not to find out why this happens, I wrote a different question for that.
Again, in this context all I want is to fix the symptoms, while I haven’t found out the cause yet.
My goal here is rebuilding the missing thumbnails.
What I’ve already tried:
- This WP SE answer.
- The Regenerate Thumbnails plugin:
- If I open the attachment details panel for that specific image and press the “Regenerate thumbnails” button, I get the following message: “ERROR: Unable to load the metadata for this attachment.”
- Running “Regenerate Thumbnails For All 4028 Attachments” works, but it takes a long time (1 hour for 4.000 pictures), even with the “Skip regenerating existing correctly sized thumbnails” option turned on.
- The Fix My Posts plugin: displays a meter which goes from 0% to 100% in a few seconds, but no difference.
- The Fix Media Library plugin: even when selecting “Regenerate attachments metadata and thumbnails”, it takes about 1 second per image. It also errors out on very large images, so for me it processes about 100 images and then stops.
I’ve also come across the following snippet that is supposed to fix the missing metadata, but it isn’t working as intended (see details after code)
/**
* fix broken image metadata so that thumbs can be regenerated
*/
function fixImageMeta() {
global $wpdb;
$sql = "
select ID from {$wpdb->posts}
where post_type="attachment"
and post_mime_type like 'image/%'
";
$images = $wpdb->get_col($sql);
foreach ($images as $id) {
$meta = wp_get_attachment_metadata($id);
if (!$meta) {
$file = get_attached_file($id);
if (!empty($file)) {
$info = getimagesize($file);
$meta = array (
'width' => $info[0],
'height' => $info[1],
'hwstring_small' => "height="{$info[1]}" width="{$info[0]}"",
'file' => basename($file),
'sizes' => array(), // thumbnails etc.
'image_meta' => array(), // EXIF data
);
update_post_meta($id, '_wp_attachment_metadata', $meta);
}
}
}
}
(source)
The above code fixes the missing thumbnails on the media library, but with some inconsistencies:
Unserialized _wp_attachment_metadata
for a previously correct image:
Array
(
[width] => 700
[height] => 430
[file] => 2019/02/filename.png
[sizes] => Array
(
[thumbnail] => Array
(
[file] => filename-150x150.png
[width] => 150
[height] => 150
[mime-type] => image/png
)
[medium] => Array
(
[file] => filename-300x184.png
[width] => 300
[height] => 184
[mime-type] => image/png
)
)
[image_meta] => Array
(
[aperture] => 0
[credit] =>
[camera] =>
=>
[created_timestamp] => 0
[copyright] =>
[focal_length] => 0
[iso] => 0
[shutter_speed] => 0
Regenerate missing _wp_attachment_metadata =>
[orientation] => 0
[keywords] => Array
(
)
)
)
Unserialized _wp_attachment_metadata
for a “fixed” image:
Array
(
[width] => 700
[height] => 430
[hwstring_small] => height="430" width="700"
[file] => filename.png
[sizes] => Array
(
)
[image_meta] => Array
(
)
)