Skip Navigation

[Resolved] Set WooCommerce Featured image from repeating custom image field

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to automatically set a post's featured image using the first image in a repeating custom image field.

Solution:
You can use the cred_save_data hook to check the repeating images and get the attachment ID of the first one, then assign that image ID to the _thumbnail_id attribute of the post to set it as the featured image.

add_action('cred_save_data', 'save_img_0_as_featured',10,2);
function save_img_0_as_featured($post_id, $form_data) {
  $forms = array( 123, 456 );
  if ( in_array( $form_data['id'], $forms ) )
  {
    $imgs = get_post_meta( $post_id, 'wpcf-rept-img');
    if(isset($imgs) && isset($imgs[0])) {
      $img = $imgs[0];
      $attachment_id = attachment_url_to_postid( $img );
      update_post_meta( $post_id, '_thumbnail_id', $attachment_id );
    }
  }
}

Change 123, 456 to be a comma-separated list of Form IDs where you want to apply this code. Then change rept-img to match your repeating image field slug.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/attachment_url_to_postid/
https://codex.wordpress.org/Function_Reference/update_post_meta

This support ticket is created 6 years, 3 months 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Nashaat 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1092400

I have an image field called "car-images" that Allow multiple-instances of this field. this field is used in cred form for new product submission. I would like to pull the first image of the multiple image field "index-0" to be saved as WooCommerce _featured_image when submitting cred form.

what is the best way to do this?

#1092467

You can use the cred_save_data hook to check the repeating images and get the attachment ID of the first one, then assign that image ID to the _thumbnail_id attribute of the post to set it as the featured image.

add_action('cred_save_data', 'save_img_0_as_featured',10,2);
function save_img_0_as_featured($post_id, $form_data) {
  $forms = array( 123, 456 );
  if ( in_array( $form_data['id'], $forms ) )
  {
    $imgs = get_post_meta( $post_id, 'wpcf-rept-img');
    if(isset($imgs) && isset($imgs[0])) {
      $img = $imgs[0];
      $attachment_id = attachment_url_to_postid( $img );
      update_post_meta( $post_id, '_thumbnail_id', $attachment_id );
    }
  }
}

Change 123, 456 to be a comma-separated list of Form IDs where you want to apply this code. Then change rept-img to match your repeating image field slug.

#1092482

Awesome, exactly what i needed. Thank you very much.