ControlledChaos
8/27/2015 - 9:28 PM

Set the featured image from the first entry in an ACF gallery field, saving only for a specified post type.

Set the featured image from the first entry in an ACF gallery field, saving only for a specified post type.

function set_featured_image_from_gallery() {

    global $post;
    $post_id = $post->ID;

    $has_thumbnail = get_the_post_thumbnail($post_id);

      if ( !$has_thumbnail ) {

        $images = get_field('image_gallery', $post_id, false);
        $image_id = $images[0];

        if ( $image_id ) {
          set_post_thumbnail( $post_id, $image_id );
        }
      }

}
add_action( 'save_post_property', 'set_featured_image_from_gallery' );