Skip to content

Instantly share code, notes, and snippets.

@mattradford
Last active January 23, 2018 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattradford/5d097d41e326ffeeee33 to your computer and use it in GitHub Desktop.
Save mattradford/5d097d41e326ffeeee33 to your computer and use it in GitHub Desktop.
Set post thumbnail from gallery. Saves a performance hit.
function set_featured_image_from_gallery() {
$has_thumbnail = get_the_post_thumbnail($post->ID);
if ( !$has_thumbnail ) {
$images = get_field('gallery', false, false);
$image_id = $images[0];
if ( $image_id ) {
set_post_thumbnail( $post->ID, $image_id );
}
}
}
add_action( 'save_post', 'set_featured_image_from_gallery' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment