Skip Navigation

[Resolved] Simple question. Can a custom field be set as a featured image for a custom post

This thread is resolved. Here is a description of the problem and solution.

Problem:
The customer wants to set a custom field as the featured image for an entire custom post type. They are looking for a solution to establish this custom field globally for each post, as they did not find an existing feature in the documentation that meets this requirement.

Solution:

We informed the customer that there is no built-in feature within Toolset to directly set a custom field as the featured image. However, we provided a workaround involving the following steps:

Use the save_post action hook to trigger a PHP function when a post is saved.
Use the get_post_meta function to retrieve the custom image field value.
Retrieve the remote image file using cURL.
Save the image into the WordPress media library using wp_insert_attachment.
Set the media ID as the post's featured image using set_post_thumbnail.
Additionally, we shared a link to a forum post where a colleague provided a working example of code that runs through all posts of a specific custom post type, assigning a given custom field as the featured image.

Relevant Documentation:

https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
https://codex.wordpress.org/Function_Reference/get_post_meta
https://codex.wordpress.org/Function_Reference/wp_insert_attachment

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 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Sao_Paulo (GMT-03:00)

This topic contains 1 reply, has 2 voices.

Last updated by Mateus Getulio 5 months ago.

Assisted by: Mateus Getulio.

Author
Posts
#2708230

Hello,

I'm figuring out if I can set a custom field as a featured image for an entire custom post type

I searched the documentation but only found a recommendation that the forms automatically establish a featured image(cred_save). Which may be an option, but if I could establish a custom field globally for each post it would be much better

Thanks greetings

#2708346

Mateus Getulio
Supporter

Languages: English (English )

Timezone: America/Sao_Paulo (GMT-03:00)

Hello there,

I understand that you'd like that when user saves a post, the custom image field gets saved as the post feature image, correct? Or to create some type of link between a given custom field and the featured image.

If it is one of those, I'm afraid there isn't such a built-in feature within Toolset.

As a workaround you could try those suggestions:
1) When user save the post, you can use action hook save_post to trigger a PHP function
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

2) Use the get_post_meta to get the custom image field value
https://codex.wordpress.org/Function_Reference/get_post_meta
- Then retrieve the remote image file
hidden link
- Save it into wordpress media library
https://codex.wordpress.org/Function_Reference/wp_insert_attachment
- Save the media ID as post feature image(product image)
https://codex.wordpress.org/Function_Reference/set_post_thumbnail

Alternatively, here my colleague shared a working example of a piece of code that runs once through all posts of that specific CPT assigning a given custom field as the featured image, it may align with what you need to accomplish, please check it out: https://toolset.com/forums/topic/setting-featured-image-using-custom-field/#post-2102075