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 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 6 years, 3 months ago.
Assisted by: Christian Cox.