Skip to content

Instantly share code, notes, and snippets.

@thefrosty
Created August 21, 2012 18:09
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 thefrosty/3417988 to your computer and use it in GitHub Desktop.
Save thefrosty/3417988 to your computer and use it in GitHub Desktop.
Autoset Featured Image
function autoset_featured() {
global $post;
if ( empty( $post ) ) return;
$already_has_thumb = has_post_thumbnail( $post->ID );
if ( !$already_has_thumb ) {
$attached_image = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => '1' ) );
if ( $attached_image ) {
$count = 0;
foreach ( $attached_image as $attachment_id => $attachment ) {
set_post_thumbnail( $post->ID, $attachment_id );
$count++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment