Skip to content

Instantly share code, notes, and snippets.

@ayublin
Last active December 24, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayublin/6806911 to your computer and use it in GitHub Desktop.
Save ayublin/6806911 to your computer and use it in GitHub Desktop.
Regenerate attachment meta data
<?php
// get all attachments
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
) );
foreach($attachments as $attach)
{
// get upload base dir
$upload = wp_upload_dir();
$upload = $upload['basedir'];
// get file relative path
$begin = strrpos($attach->guid, 'uploads/') + strlen('uploads/');
$path = trailingslashit($upload) . substr($attach->guid, $begin);
// generate and update meta data
$data = wp_generate_attachment_metadata( $attach->ID, $path );
wp_update_attachment_metadata( $attach->ID, $data );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment