Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hdragomir/953273 to your computer and use it in GitHub Desktop.
Save hdragomir/953273 to your computer and use it in GitHub Desktop.
Set images within posts as their thumbnails
<?php
$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);
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment