Support

Account

Home Forums General Issues Setting custom field image as featured image

Solved

Setting custom field image as featured image

  • Hi there – I am looking to set an image custom field as a featured image for a custom post type

    I have changed the field name to mine (resource_image) but no luck

    I have tried adapting some code found in this thread: https://support.advancedcustomfields.com/forums/topic/set-image-as-featured-image/

    function acf_set_featured_image( $value, $post_id, $field  ){
        
        if(isset($value) && is_array($value)){
          delete_post_thumbnail( $post_id);
          //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[0][resource_image]);
        }
     
        return $value;
    }
    
    // acf/update_value/name={$field_name} - filter for a specific field based on it's name
    add_filter('acf/update_value/name=resource_image', 'acf_set_featured_image', 10, 3);
    

    Any idea what I am doing wrong

  • Did you try the code exactly as Jonathan posted it and only changing the field name?

    
    <?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.

  • John…..

    I’ve been using custom code to do this for years and now your solution seems to obvious! Thank you for posting this.

  • Although, that said, it doesn’t seem like a lot of work for Elliot to just have a toggle on the image field that sets the image as “featured” (and takes away the ability to set the slug). 🙂

  • Yes, it was too obvious. I mentally smacked myself in the head when I saw it because like everyone else I’ve been using an acf/save_post action on pretty much every site I’ve built since starting to use ACF, quite a few years ago now. Simply because I wanted to make the featured image required and be able to enforce specific image sizes for different sites/post types.

    The only real problem with using this method is that get_fields() will not return this field. ACF ignores all fields that start with an underscore. These fields are also hidden by WP. But since I’ve never used get_fields() it works out fine for me.

  • Yeah, though I suppose the implication would be that you want to use the_post_thumbnail() (and other related functions) anyway, if you’re trying to auto-set the featured image, so not a huge downfall.

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

You must be logged in to reply to this topic.