Set Featured image from first image attachment

By: Mariyan Hristov | Asked: 05/17/2023
ForumsCategory: Code HelpSet Featured image from first image attachment
Mariyan Hristov asked 1 year ago

Hi   I saw in the documentation that if we have multiple images file upload field the create post functionality may not work well with attaching an image to the featured image. Yes it doesn't work. So I tried adding this function in the functions.php file:  

function prefix_auto_featured_image() {
    global $post;
    if (!has_post_thumbnail($post->ID)) {
        $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
      if ($attached_image) {
              foreach ($attached_image as $attachment_id => $attachment) {
                   set_post_thumbnail($post->ID, $attachment_id);
add_action('the_post', 'prefix_auto_featured_image');
add_action('save_post', 'prefix_auto_featured_image');
add_action('draft_to_publish', 'prefix_auto_featured_image');
add_action('new_to_publish', 'prefix_auto_featured_image');
add_action('pending_to_publish', 'prefix_auto

if ( function_exists( 'add_theme_support' ) ) {

add_theme_support( 'post-thumbnails' ); // This should be in your theme. But we add this here because this way we can have featured images before swicth to a theme that supports them.

function easy_add_thumbnail($post) {
$already_has_thumb = has_post_thumbnail();
$post_type = get_post_type( $post->ID );
$exclude_types = array('');
$exclude_types = apply_filters( 'eat_exclude_types', $exclude_types );

// do nothing if the post has already a featured image set
if ( $already_has_thumb ) {

// do the job if the post is not from an excluded type
if ( ! in_array( $post_type, $exclude_types ) ) {
// get first attached image
$attached_image = get_children( "order=ASC&post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );

if ( $attached_image ) {
$attachment_values = array_values( $attached_image );
// add attachment ID
add_post_meta( $post->ID, '_thumbnail_id', $attachment_values[0]->ID, true );

// set featured image before post is displayed (for old posts)
add_action('the_post', 'easy_add_thumbnail');

// hooks added to set the thumbnail when publishing too
add_action('new_to_publish', 'easy_add_thumbnail');
add_action('draft_to_publish', 'easy_add_thumbnail');
add_action('pending_to_publish', 'easy_add_thumbnail');
add_action('future_to_publish', 'easy_add_thumbnail');
add_action('future_to_publish', 'prefix_auto_featured_image');

  I am creating the posts in Draft mode so I can approve them. So when I receive the entry I make a post update and it should get the first image from the attachments as a featured. Yes, but it's not working unless I remove an invisible featured image that is attached by default and update the post. It's quite ok, but in the Formidable entries the image attachments are lost and they are visible only in the post.   Would be grateful for an advice how I can achieve a dynamic featured image from a multiple file upload field.   Thanks, Mariyan

1 Answers
Mariyan Hristov answered 1 year ago

Ok I tricked it.   Since I saw that an empty image was being attached to every post coming from the form. I just added a line in the beginning of my function

$delete_featured_image_first = delete_post_meta( $post->ID, '_thumbnail_id' );So that all featured images are removed first and then attached.There is an option to exclude post types  as well..

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community