Support

Account

Home Forums ACF PRO Use first image from gallery of custom post type to set featured image

Solved

Use first image from gallery of custom post type to set featured image

  • How do I set the first gallery image from a custom post type gallery field as the featured image?

    I found another post similar to this but it is not for a custom post type. This is the code I’m assuming only for a normal post:

    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('model_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' );

    I have a custom post type named “models” that has a gallery field named “models_gallery”

    I want to automatically set the first image from the gallery field to also be the featured image for the post.

  • That will work for a custom post type. It’s based on the post ID and the field name.

  • For some reason the code did not work. I did find the following code that did work.

    add_action('acf/save_post', 'flex_FeaturedImageSetByACF', 50);
    
    function flex_FeaturedImageSetByACF() {
    
        $current_screen         = get_current_screen(); // Current admin screen needed to identify the current cpt
        $current_cpt_name       = 'models'; // Current cpt name
        $current_cpt_support    = 'thumbnail'; // We want to check if the CPT supports this feature
    
        global $post;
    
        $post_id                = ( $post->ID ); // Current post ID
        $post_gallery_field     = get_field('model_gallery', $post_id ); // ACF field
    
        if  ( !empty( $post_id ) ) {
    
            if ( isset( $post_gallery_field['0'] ) ) {
    
                $post_image_id          = $post_gallery_field['0']['id']; // ACF image filed ID
                $post_image_url         = $post_gallery_field['0']['url']; // ACF image filed URL
    
                // If current cpt supports thumbnails/featured images
    
                if ( post_type_supports( $current_cpt_name, $current_cpt_support ) ) {
    
                    if ( ( $post_image_url ) AND ( ( $post_image_url ) != ( get_the_post_thumbnail() ) ) ) {
    
                        update_post_meta($post_id, '_thumbnail_id', $post_image_id);
    
                    }
    
                }
    
            } else {
    
                update_post_meta( $post_id, '_thumbnail_id', 0 );
    
            }
    
        }
    
    }
  • This looks amazing. Any thoughts on how this could be utilized as a mu-plugin?
    Thanks for any help.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Use first image from gallery of custom post type to set featured image’ is closed to new replies.