Hi Samii,
I was also struggling to solve the same situation to set the first image as a featured image and I was able to successfully do it by adding following code in my theme’s functions.php
file. Here is the code that will set every 1st uploaded image as a featured image of the post when it is PUBLISHED:
//Auto Set 1st Image of Event as a Featured Image
function auto_add_featured_image_events($new_status, $old_status, $post) {
global $post;
if ($post->post_type == 'event_listing') {
$data = get_post_meta( $post->ID, '_event_banner', false);
foreach ($data as $key=>$image) {
// set the image url
$image_url = $image[0];
// store the image ID in a var
$image_id = attachment_url_to_postid($image_url);
if($new_status == 'publish') {
//Update the 1st Image of event as featured Image
update_post_meta( $post->ID, '_thumbnail_id', $image_id );
}
}
}
}
add_action( 'transition_post_status', 'auto_add_featured_image_events', 1, 3);