Skip Navigation

[Resolved] Split: Can't fill Featured Image with custom field first image – repeating field sort order does not get saved correctly

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
#2652149

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

#2652153

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

This is a known issue and fortunately we have workaround for that. You should try to use the following code snippet to save the correct sort order for your image field with your edit post form.

add_action('cred_save_data', 'ts_save_the_rf_sort_order_manually',10,2);
function ts_save_the_rf_sort_order_manually($post_id, $form_data) {
    // if a specific form
    if ($form_data['id']==99999) {
    		
      	$field_slug = 'images';

    	$field_content = $_POST['wpcf-'.$field_slug];
      		
      		$sort_order = array();
      	    
      		delete_post_meta($post_id,'wpcf-'.$field_slug);
      
      		foreach($field_content as $k=>$v):
      			$sort_order[] = add_post_meta($post_id,'wpcf-'.$field_slug,$v);
    		endforeach;
   
      		update_post_meta($post_id,'_wpcf-'.$field_slug.'-sort-order', $sort_order);

             $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] );
           
    }
}

Where:
- Replace 99999 with your original edit form ID

Can you please confirm it works as expected now.

#2652201

Hi Minesh,
This works perfectly and the Featured Image is ok too ! thanks.
I will close the ticket, but first could you please explain how does the system know which snippet to run first when there are many for the same form ?
thanks
Kind regards
Nicola

#2652205

Minesh
Supporter

Languages: English (English )

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

You should try to combine multiple "cred_save_data" hook within one and switch based on the form ID using if condition and if you have multiple hooks added it will catch all hooks and run accordingly based on its set priority.