Advanced Custom Fields Set Image Field as Post Featured Image

Are you trying to set the image field as a post featured image on your WordPress website? Then here we have a simple solution to do that. To set the image field as featured thumbnail for your post you need to use acf/save_post.
Use the below function in your WordPress functions.php file. This function will help you to save the image field as a post featured image with the help of ACF image field name and _thumbnail_id.
1 2 3 4 5 6 7 8 9 10 11 |
add_action('acf/save_post', function ($post_id) { $value = get_field('post_image', $post_id); if ($value) { if (!is_numeric($value)) { $value = $value['ID']; } update_post_meta($post_id, '_thumbnail_id', $value); } else { delete_post_meta($post_id, '_thumbnail_id'); } }, 11); |
Note: You need to replace post_image to which you create an image field name in the above function
Conclusion:
I hope this article will help you to set the image field as a featured image on your WordPress website. If you have any doubts about this topic let me know in the comment section and if you love this article share it with your friends.
ACFAdvanced Custom FieldsWithout Pluginwordpress
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]