Aleš Sýkora / January 9, 2024 / 3 comments

Use first ACF Gallery image as Featured image of Custom post type single

Post summary: How do I set the first gallery image from a custom post type gallery field as the featured image? Found this here: Use first image from gallery of custom post type to set featured image – ACF Support (advancedcustomfields.com) It use ACF | acf/save_post (advancedcustomfields.com) filter to apply. Regenerate all posts to set the featured…

How do I set the first gallery image from a custom post type gallery field as the featured image? Found this here: Use first image from gallery of custom post type to set featured image – ACF Support (advancedcustomfields.com)

It use ACF | acf/save_post (advancedcustomfields.com) filter to apply.

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'; // Your 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 ); // Your ACF gallery fields slug

    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 );

        }

    }

}

Regenerate all posts to set the featured images right

Put the code below in functions.php and reload the page, or use code snippets plugin, set this code to run at init and refresh page. After it runs once, all posts of chosen post type will be updated. Don’t forget to disable the code snippet then or delete the code from functions.php!

BACKUP your database first!

<?php

function update_product_featured_images() {
    $args = array(
        'post_type'      => 'models', // Your post type name
        'posts_per_page' => -1,        // Load all posts
    );

    $posts = new WP_Query($args);

    if ($posts->have_posts()) {
        while ($posts->have_posts()) {
            $posts->the_post();
            $post_id = get_the_ID();
            $post_gallery_field = get_field('galerie_produktu', $post_id); // your ACF gallery field slug

            if (!empty($post_gallery_field) && isset($post_gallery_field[0])) {
                $post_image_id = $post_gallery_field[0]['id'];
                update_post_meta($post_id, '_thumbnail_id', $post_image_id);
            }
        }
    }
}

// Run this function once to update featured image for all posts
update_product_featured_images();

Fuel my passion for writing with a beer🍺

Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻

3 comments

Share Your Thoughts