Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Set images within posts as their thumbnails
$thumbnail_width = 593;
$thumbnail_height = 400;
foreach(query_posts('posts_per_page=-1') as $post)
if(! has_post_thumbnail($post->ID))
foreach(get_children('post_type=attachment&post_mime_type=image&post_parent=' . $post->ID) as $image){
$sizes = wp_get_attachment_metadata($image->ID);
if($sizes['height'] >= $thumbnail_height && $sizes['width'] >= $thumbnail_width){
set_post_thumbnail($post->ID, $image->ID);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment