Support

Account

Home Forums ACF PRO Use a current ACF image as the featured image

Solved

Use a current ACF image as the featured image

  • Hi All

    I currently have a custom post type which does not have the featured image box enabled (it was in the early days of learning ACF)

    Now we have a lot of posts which have images but are all stored as ACF images i.e the main image.

    I want to be able to just update the featured image with the one that is set as the main_image so we don’t have to go back and add featured images to all these posts or change the code away from main_image to the feature image.

    Having a look it seems like having to hook into the on save but hooks are pretty new to me so some help would be really appreciated.

  • I have managed to work it out here is the answer if anyone needs it. This would go in your function.php file

    // retrieves the attachment ID from the file URL
    function get_image_id($image_url) {
        global $wpdb;
        $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
        return $attachment[0];
    }
    
    //Set the featured image on save of the post
    add_action('save_post', 'set_featured_image_from_image');
    
    //set featured image as main image.
    function set_featured_image_from_image()
    {
            $has_thumbnail = get_the_post_thumbnail($post->ID);
            
            //If there is not featured image get the other image.
            if (!$has_thumbnail) {
            //get the url of the ACF image and get the ID of that image
                $image_url = get_field('main_image');
                $image_id = get_image_id($image_url);
    
                if ($image_id) {
                    set_post_thumbnail($post->ID, $image_id);
                }
            }
        
    }

    I am outputting just the url from the image so I had to reverse get the ID. Those who are using the full object should be able to get the ID more easily.

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

  • Hello John.
    Thanks for this solution, but if I’ve got an existing image field and I replace the name field from my field group, all my old images go away.
    How can I keep them if I change the field name?

    Thanks in advance!

  • Hello @hube2
    Could you tell me anything about it to fix this issue?

    Thanks!

  • There isn’t any easy way to change the field name of a field once it has been used. The use of “_thumbnail_id” for the field name is something that you really need to do from the start before any data is added to a site.

    In order to update every post you would need to do a query to get every post, loop through them, get the value from the ACF field and then update the post thumbnail field.

  • Admittedly a very old post but I have found an easy way to solve this:

    Use the ‘Better Search Replace’ plugin

    Use the field name of the acf image field in the find, use _thumbnail_id in the replace. Apply this to the tables wp_posts and wp_postmeta

    * Take a backup before trying this *

  • the issue with the solution given by @jperformance is that ACF fields are not required to be unique. It will only work if the field name that you want to change is unique. And you also need to change another meta key that holds the field key reference
    "_{$old_field_name}" to ‘"_{$new_field_name}"

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

The topic ‘Use a current ACF image as the featured image’ is closed to new replies.