Skip Navigation

[Resolved] Affect an image field as featured image for a product

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

Problem:
A CRED form is used to publish custom posts and includes a custom image field. The client would like this image to also be assigned as the featured image when the form is submitted.

Solution:
You can use the cred_save_data hook to run a snippet of code when the form submits which will set the _thumbnail_id post meta with the ID of the media post created by the custom image field. Example code is shown below: https://toolset.com/forums/topic/affect-an-image-field-as-featured-image-for-a-product/#post-513008

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#csd

This support ticket is created 7 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Pat 7 years ago.

Assisted by: Nigel.

Author
Posts
#512983

Pat

Hello,

I have created a custom image field (wpcf-image-princ) for a product and a Cred form where users can create products in the frontend. In order to simplify the creation, I would like to use this custom image field (wpcf-image-princ) as feature image (meaning, when the user fills the custom image field, then both (wpcf-image-princ) and product features image are saved with the uploaded image).
How can I do this?
Regards
Pat

#513008

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Pat

You can use the cred_save_data hook to set the _thumbnail_id post meta for the published post.

When you add a custom image field to your form the image is added as a post. The field stores the URL of the image file, but we need the ID of the related post, so we need a little custom code snippet to get that.

Here's what you need (I trust you can edit the slug of your custom field, and note the hook I use contains the id of the form to save testing for it):

/**
 * Set custom image field as featured image
 */
function custom_featured_image( $post_id, $form_data ) {

	// get the custom image field (stored as url)
	$image_url = get_post_meta( $post_id, "wpcf-custom-image", true );

	// get the id of this image post
	global $wpdb;
	$attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));

	// set the _thumbnail_id post meta with this id
	update_post_meta( $post_id, '_thumbnail_id', $attachment[0] );

}
add_action( 'cred_save_data_16', 'custom_featured_image', 10, 2 );
#513093

Pat

Hi NIgel
Only one word : Perfect!
Regards
Pat

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.