phpwordpressadvanced-custom-fieldswordpress-featured-image

Featured image via built-in Wordpress function instead of ACF (Pro) image field


I have a news website with over 3000 posts. The previous website builder did not use the built-in functionality of Wordpress for the featured image, but used an ACF (Pro) image field instead.

On the new website I would like to use the standard function of Wordpress. I just have no idea how to set the images from the ACF (Pro) image field as a featured image via the built-in Wordpress function.

Is there a script that can do that automatically? I don't like having to do more than 3000 messages manually.

So the image should be from this:

enter image description here

To this:

enter image description here

I've searched all over the internet for a solution to this, but couldn't find anything.

Thank you very much in advance for any help.


Solution

  • We can make a simple script to fix your post thumbnails.

    For easiest implementation lets just add this function to your functions.php which will run when we your site loads.

    You want to run this on over 3000 news posts. This means your site will take a few moments, maybe minutes, to run the script before your site eventually loads.

    If you are using this on a live environment, lets add a URL parameter to only allow this to run when param is true, for example...

    https://www.example.com/?fix_post_thumbs=true

    Here is the code you need to add to your functions functions.php...

    Please read comments in code so you know what is happening.

    // fix post thumbs
    function fix_post_thumbs() {
    
        // if current user is admin and url param fix post thumbs is set
        if(current_user_can('administrator') && isset($_REQUEST['fix_post_thumbs'])) {
    
            // if url param for fix post thumbs is true
            if($_REQUEST['fix_post_thumbs'] === 'true') {
    
                // our wp query args for which we want to run this script
                // change post type value to your news post type
                $args = array (
                    'post_type'         => 'post',
                    'post_status'       => 'any',
                    'posts_per_page'    => -1
                );
    
                // count the process posts
                $count = 0;
    
                // set our wp query
                $query = new WP_Query($args);
    
                // if we have posts to loop
                if($query->have_posts()):
    
                    // loop through our query post results
                    while ($query->have_posts()): $query->the_post();
    
                        // get our acf featured image field attachment id
                        // 3rd parma must be false just so it returns id
                        // you must change 'acf_featured_img' field name to your acf image field name
                        $attachment_id = get_field('acf_featured_img', $query->post->ID, false);
    
                        // if there is an acf featured attachment id set
                        if($attachment_id) {
    
                            // set the post thumbnail with acf featured image attachment id
                            set_post_thumbnail($query->post, $attachment_id);
    
                            // count this process
                            $count++;
    
                        }
    
                    endwhile;
    
                    // output message showing count of featured images set
                    echo '<pre>' .  print_r($count . ' featured images have been set.', true) . '</pre>';
                
                else :
    
                    // no posts found message
                    echo '<pre>' .  print_r('Sorry, no posts matched your criteria.', true) . '</pre>';
    
                endif;
    
            }
    
        }
    
        // finally return
        return false;
    
    }
    
    // run our fix post thumbnails
    fix_post_thumbs();
    

    This is not tested, you may want to test on some specific news post ids first via WP_Query, using post__in to only run this on selected posts. 👍🏼


    Update: I've added a print_r() information showing count of how many featured images have been set once process has completed.


    Plugin Update: As suggested I've created a simple plugin which you access via the Tools menu when plugin is activated.

    enter image description here

    Simply select the post type you wish to run this function on, then select the ACF image field name you want to set as the post_thumbnail, and click run.

    If ACF image field for current post has no attachment value, the function will skip this post and continue processing.

    enter image description here

    See git repo plugin link below...

    https://github.com/joshmoto/acf-image-set-post-thumbnail

    Or download this distribution version below to install via uploading zip file to your plugins...

    acf-image-set-post-thumbnail.zip