Skip Navigation

[Resolved] Assign first uploaded image from repeating image field as featured

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 1 voice and has 0 replies.

>
Author
Posts
#2691523
3.PNG
2.PNG
1.PNG

Hi,
I'm building a website for a client and one of the requirements is to allow the user to upload posts with images.
I've tried the following code(https://toolset.com/forums/topic/use-first-uploaded-image-as-featured-image/), but it's not working.
My form id (5645) and field (more-pic) are inserted, but when I run it trough, I get error and it cannot be activated.

add_action('cred_save_data', 'set_image_as_featured',10,2);
function set_image_as_featured($post_id, $form_data)
{// if a specific form
if ($form_data['id']==5645)
{ if (isset($_POST['wpcf-more-pic']))
{ $image = $_POST['wpcf-more-pic'][0];
// Save the first image as featured image
set_post_thumbnail($post_id,attachment_url_to_postid($image)); } }}

Website: hidden link
Currently I'm using the standard featured image field in addition to the repeating images, but it's not user friendly to ask to upload several images trough different fields, so the feature image of the post shows.

#2691536

Edit: It appears I've deleted the php head by mistake. It's working now

#2691537

Edit: It appears I've deleted the php head by mistake. It's working now