Support

Account

Home Forums General Issues Set featured image from ACF image field

Solved

Set featured image from ACF image field

  • I am looking for a way to automatically set the featured image of a post or custom post type, to the image uploaded on a custom image field.

    any ideas?

    thanks
    Andy

  • This is covered pretty well here https://support.advancedcustomfields.com/forums/topic/set-image-as-featured-image/ let me know if you have any questions.

  • yes the code is – works with custom post types too:

    <?php
     
    function acf_set_featured_image( $value, $post_id, $field  ){
        
        if($value != ''){
    	    //Add the value which is the image ID to the _thumbnail_id meta data for the current post
    	    add_post_meta($post_id, '_thumbnail_id', $value);
        }
     
        return $value;
    }
    
    // acf/update_value/name={$field_name} - filter for a specific field based on it's name
    add_filter('acf/update_value/name=cursusfoto', 'acf_set_featured_image', 10, 3);
     
    ?>
  • I know this is an old topic, but after doing this for some time I’ve just recently realized that using a filter isn’t needed. All you really need to do is create an image field with the field name of “_thumbnail_id”, and ACF will just update the featured image for you. There’s no error checking, but simply making your image field required deals what that problem.

    Just thought I add this for anyone that finds this topic.

    • Vasili

    • May 4, 2017 at 8:31 pm

    @hube2

    John Huebner,

    Just found this topic, thank you for writing this!

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.