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:

Relevant Documentation:

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.


This topic contains 2 replies, has 2 voices.

Last updated by Pat 3 years, 5 months ago.

Assigned support staff: Nigel.




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?



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 );


Hi NIgel
Only one word : Perfect!