Skip Navigation

[Resolved] Can't fill Featured Image with custom field first image

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 3 replies, has 2 voices.

Last updated by Minesh 1 year, 1 month ago.

Assisted by: Minesh.

Author
Posts
#2651587

Hi,
some time ago somebody from your team wrote for me this snippet that is supposed to automatically fill the field Excerpt with some custom fields data and the featured image with the first image from the images custom field. I noticed that the Excerpt field is updated, but not the featured image.
I tried to change

types_render_field("images", array('item'=> $post_id, 'index' =>0, 'url' => true)); //gets first image

into

types_render_field("wpcf-images", array('item'=> $post_id, 'index' =>0, 'url' => true)); //gets first image

with no success and also change that part of the code with this (found in another support ticket):

$image = $_POST['images'][0];
// Save the first image as featured image
set_post_thumbnail($post_id, attachment_url_to_postid($image));

No results.
Here is the complete snippet:

add_action('cred_save_data','write_excerpt_classified',10,2);
function write_excerpt_classified($post_id,$form_data) {
	if ($form_data['id']==78) {

	$price = types_render_field("price", array('item'=> $post_id ));
    $prov = do_shortcode( "[wpv-post-taxonomy item='".$post_id."' type='geo' separator=',' format='name'  ]");
    $brand = do_shortcode( "[wpv-post-taxonomy item='".$post_id."' type='brand' separator=',' format='name'  ]");

      // Image in featured image
$imageurl = types_render_field("images", array('item'=> $post_id, 'index' =>0, 'url' => true)); //gets first image
$imageid = attachment_url_to_postid($imageurl);
set_post_thumbnail($post_id,$imageid);  

	$excerpt= $price ."€ | Zona: " . $prov;
	$args = array('ID' => $post_id, 'post_excerpt' => $excerpt);
	wp_update_post($args);
	}
}

Could you please let me know what's wrong with the image code section ?
thanks
Regards
Nicola

#2651739

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please try to use the following code and try to resolve your issue:

add_action('cred_save_data','write_excerpt_classified',10,2);
function write_excerpt_classified($post_id,$form_data) {
    if ($form_data['id']==78) {
 
    $price = types_render_field("price", array('item'=> $post_id ));
    $prov = do_shortcode( "[wpv-post-taxonomy item='".$post_id."' type='geo' separator=',' format='name'  ]");
    $brand = do_shortcode( "[wpv-post-taxonomy item='".$post_id."' type='brand' separator=',' format='name'  ]");
 

// set the _thumbnail_id post meta with this id
    
    $image_url = get_post_meta($post_id, 'wpcf-images', true);

      global $wpdb;
      $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ));
     update_post_meta( $post_id, '_thumbnail_id', $attachment[0] );
    
    $excerpt= $price ."€ | Zona: " . $prov;
    $args = array('ID' => $post_id, 'post_excerpt' => $excerpt);
    wp_update_post($args);
    }
}
#2651917

Hello Minesh,
the snippet now works perfectly, thanks. I just noticed that swapping the order of the images in the edit form after saving the order doesn't change in front view and the snippet of course keeps the image set as first on post creation.
Is this a Toolset bug ?
thanks
Regards
Nicola

#2652151

Minesh
Supporter

Languages: English (English )

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

Glad to know that the solution I shared help you to resolve your original issue of saving correct featured image.

As per our support policy, we entertain only one question per ticket. This will help other users searching on the forum as well as help us to write correct problem resolution summery.

May I kindly ask you to open a new ticket with every new question you may have in future. For now, I will split the ticket with your question and we will continue with the following split ticket:
- https://toolset.com/forums/topic/split-cant-fill-featured-image-with-custom-field-first-image-repeating-field-sort-order-does-not-get-saved-correctly/

You're welcome to mark resolve this ticket.