ControlledChaos of Controlled Chaos Design
7/20/2017 - 4:31 PM

Set the featured image from the first image in an ACF gallery. Saves only for a specified post type.

Set the featured image from the first image in an ACF gallery. Saves only for a specified post type.

<?php

function ccd_featured_image_from_gallery() {
    global $post;
    $post_id       = $post->ID;
    $has_thumbnail = get_the_post_thumbnail( $post_id );

      if ( ! $has_thumbnail ) {

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

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

}
add_action( 'save_post_{$post-type}', 'ccd_featured_image_from_gallery' );

?>

Featured Image from ACF Gallery

WordPress Snippet + ACF