Skip Navigation

[Resolved] Use first uploaded image as featured image

This support ticket is created 3 years, 11 months 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 13 replies, has 2 voices.

Last updated by JosV9233 3 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#1637273

On a post form I use an image upload field and allow multiple instances of this field. How can I use the first uploaded image as the (standard WordPress) featured image?

#1637641

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jos,

Thanks for getting in touch.

This would require the use of custom coding when the form is being submitted to pick out the first image from the array of images being sent.


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']==13)
    {
        if (isset($_POST['wpcf-repeatable-images']))
        {
          $image = $_POST['wpcf-repeatable-images'][0];
            // Save the first image as featured image
      		set_post_thumbnail($post_id,attachment_url_to_postid($image));
           
        }
    }
}

Add the above to your toolset custom code page in Toolset -> Settings -> Custom Code. Once you've added the code you will need to replace the 13 with ID of your form and the "wpcf-repeatable-images" with the slug of your repeatable image field keeping the wpcf- prefix.

Once you have done this you will need to activate the snippet.

Please let me know if this helps.
Thanks,
Shane

#1638209

Hi Shane,
Thank you for your quick reaction. However the featured image does not appear.
This is probably due to the fact that I use an image field where I allow multiple instances. I would not know how to use a repeatable image field.
Could you enlighten me?
Thamks,
Jos

#1639103

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jos,

A multiple instance field is the same as a Repeatable field.

Can you send me a link to the form on the frontend ? As well as a screenshot of the code that you've added the custom code section of your site.

Thanks,
Shane

#1639523

Hi Shane,

This is the code I added: hidden link
To see the form you'll have to login (the form allows members to post without reviews). So please send me a reply which will be invisible.

Jos

#1640083

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jos,

Thank you for the screenshot.

I've enabled the private fields so that you can send me admin access to check on why this isn't working.

Thanks,
Shane

#1641793

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jos,

I believe I spot the issue. In the code you have the form id being 227 when the actual ID of the form is 276.

Could you update this in the code and let me know if it helps.

Secondly i'm not sure which page you've added the form itself on so if changing the ID doesn't please let me know the page the form is on so that I can test this out.

Thanks,
Shane

#1641941

Hi Shane, sorry for messing things up.

The form is, as you said, id:276 is on page "maak een nieuw bericht" id: 270. It fills the CPT biljartberichten. You'll find the archive for biljartberichten in the menu under 'nieuws'.
There is something strange with the attached images (slug= 'afbeeldingen'). In the backend they show with the respective posts (biljartberichten). On the frontend they only show with the first post 'testfoto'.

I hope you can get this cleared.

#1641961

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jos,

The code should now be working, there was also an error with the custom field slug. You have wpfc- when it should've been wpcf-

Please let me know if this helps.
Thanks,
Shane

#1641983

Oh yes, it was not working yet.

#1641993

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jos,

Take a look at this post here.
hidden link

I just created this with the frontend form and as you can see the first image uploaded is added as the featured image.

Thanks,
Shane

#1641997

Shane,
Thank you very much. This did the trick.
Very nice support again!

#1642147

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Jos,

Happy I was able to help you out with this one.

If there are no further queries on this then you can go ahead and mark this ticket as resolved.

Thanks,
Shane

#1642153

My issue is resolved now. Thank you!

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