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

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

  • @hube2

    John Huebner,

    Just found this topic, thank you for writing this!

  • The more I use ACF, the more I like (and understand) it but now I’ve got a small problem.
    I need to copy the image-url from the options page to the featured image of a new post. The image is already uploaded. My code is not functioning… Can you give me a hint?

    PS: field_5dc3135e281b9 is the fieldkey and _thumbnail_id is the fieldname of the option field with the image.

    <?php
    function acf_set_featured_image( $value, $post_id, $field ){
    $value = get_field(‘_thumbnail_id’, ‘options’); // get image from optionspage
    add_post_meta(‘$post_id’, ‘_thumbnail_id’, $value);
    return $value;
    }
    add_filter(‘acf/update_value/key=field_5dc3135e281b9’, ‘acf_set_featured_image’, 10, 3);
    ?>

  • @John Huebner’s response is the most accurate without having to add code to the functions.php file.

  • @hube2 Its possible to do this with multiple feature images.

    My goal is, I have a post with acf with 3 dropdown options
    So depending of which option I choose I will automatically share a image in social media when I post. In a normal scenario, wordpress will take featured image as thumbnail for social media.
    But its possible to attach an image to each dropmenu option in acf?

  • WP only allows a single featured image per post, so you can’t add alternates.

  • Hi,

    I am trying to achieve this but from a field inside an ACF Block and it is not working. What would be the correct way to do this?

    Thanks.

  • I know it’s been a while since anyone has posted on this and most of what I can find nowadays is pretty ancient. Has this ever been updated with any of the ACF pro releases or do I still have to put “_thumbnail_id” in the image field?

  • It still works, although I do not know if it will work with blocks as @ybdigital is looking to do. I do not use blocks. However, I would suspect that it will not work. Normal ACF fields save data to the postmeta table. Fields in blocks save data to post_content.

  • I’m just using it with the default WP classic editor. I’m doing ACF Pro with CPT UI. But I think I found a workaround for now that looks to be doing the trick by using WP User Frontend and creating a form to match my custom fields.

  • I am new and still learning. Can someone simplify this for me. What parts of this functions do I need to change to make this work?

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

    ?>

  • Guys pleaseeeee can you help me

    I am looking to do the same thing. Whwn i use wp all import it downloads the image from an external source how do i get my gallery field to also upload that same image that just got downloaded

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

The topic ‘Set featured image from ACF image field’ is closed to new replies.