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 5 months ago.
Assisted by: Mateus Getulio.