Skip Navigation

[Resolved] Set Featured Image from Repeating Image Field

This support ticket is created 3 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 14 replies, has 2 voices.

Last updated by ahmedO-2 3 years ago.

Assisted by: Minesh.

Author
Posts
#2350187

I would like to set the Featured Image of a custom post type from the first image of a repeating image field. I found something related in this thread: https://toolset.com/forums/topic/setting-featured-image-using-custom-field/

But that seems to be for a single image field. How can I get that script to work for a repeating image field? Perhaps make the first image of that field the Featured Image of the post?

Thanks!

#2350341

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I would like to know first at what action you want to set the featured image based on repeating field's first instance, is it while using the Toolset form or from backend admin?

#2351129

Hello Minesh,
I would like this to happen when new pages are created and Saved. If I can run it once for pages that are already created, that would be great.

Thanks!

#2351701

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You mean page or post belongs to custom post type?

There is no automatic feature to add feature image for existing posts but you will require to update post or it will require the custom script that will set the feature image for existing posts/page.

If you can share details and admin access and tell me for what post type you want to set feature image based on repeating field I will review it and guide you the possible solutoin.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2354153

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you for sharing access details but when I try to login I can only access the profile page. Could you please grand full admin rights to the user you shared and get in touch with me once you do that.

#2354907

Hello Minesh,
I just verified, the username and credentials I provided is an Administrator. Please try again. Thanks!

#2356163

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please test now.

I've added the following code to "Custom Code" section offered by Toolset with code snippet "set-first-image-as-featured-image":
=> hidden link

add_action( 'save_post', 'func_set_post_thum_based_on_custom_image_field',100,3);
function func_set_post_thum_based_on_custom_image_field( $post_id, $post, $update){
    if ( $post->post_type == 'gallery' ) {
        $url = get_post_meta($post_id, 'wpcf-gallery-image2',true);
    	  set_post_thumbnail($post_id,attachment_url_to_postid($url));
      
    	
    }
}

Can you please check when you add/update the post there is post thumbnail set automatically based on the repeating image custom field.

#2356537

Hello Minesh,
That worked great and I got it to work once on the production site as well. But, I resaved the post type and now the script is not working. It only worked once. Then I saved all the posts to create the featured image, but it stopped working.

Can you tell me what some of those numbers mean, like here: // if a specific form
if ($form_data['id']==77118)
Do I need to update that number for the production environment?

Thanks!

#2356837

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - 77118 is the form ID. You should check the form ID on your production site and let me know how it goes.

If it does not work then please share the steps I need to follow to see the issue as well as admin access details to your production site.

I have set the next reply to private which means only you and I have access to it.

#2356891

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I checked the following post: hidden link

If I delete the featured image and resave the post it does set the feature image automatically. Can you please guide me what I'm missing here.

#2357211

Hello Minesh,
Thank you for looking into this. That's one of the posts where the script worked, but there are several hundred where it's not working. If you look on further pages, there are several hundred where it does not work. Here are some examples:
hidden link
hidden link
hidden link

Thanks!

#2358993

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've adjusted the code to "Custom Code" section as given under:

add_action( 'save_post', 'func_set_post_thum_based_on_custom_image_field',100,3);
function func_set_post_thum_based_on_custom_image_field( $post_id, $post, $update){
    if ( $post->post_type == 'gallery' ) {
     global $wpdb;
          $url = get_post_meta($post_id, 'wpcf-gallery-image2',true);
      
      $image_post_id = $wpdb->get_var($wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE guid = %s",
$url
));
      
    	  set_post_thumbnail($post_id,$image_post_id);
      
    	
    }
}

Can you please check now if it works as expected.

#2359553

Hello Minesh,
Yes, it's working perfectly now. Thank you very much for your help. What was the issue?

#2359555

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

The issue was with previous code snippet we were using attachment_url_to_postid() function to get the ID based on the image URL but few image URLs having special characters and that is why the function attachment_url_to_postid() failed.

So, I've adjusted the code and we fire a query to get the image ID based on URL and it works as expected. 🙂

#2360219

My issue is resolved now. Thank you very much Minesh!