Skip Navigation

[Closed] Set custom image field as custom post type's featured image via code snippet

This support ticket is created 4 years, 2 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 13:00 – 18:00 13:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Tagged: 

This topic contains 1 reply, has 2 voices.

Last updated by Raja Mohammed 4 years, 2 months ago.

Assisted by: Raja Mohammed.

Author
Posts
#1837015

Tell us what you are trying to do?

Almost all my custom post types have a custom image field called "representational image". I am using the Divi theme. What I would like is that the "representational image" is automatically set as the feature image for each post type. Note that I am using frontend post forms to add in content, so when I add or update content using frontend forms, a code snippet should be triggered that makes the representational image field's value the same as the featured image field's value.

Is there any documentation that you are following?
I looked everywhere but did not find a definite solution.
Is there a similar example that we can see?

What is the link to your site?

#1838371

Raja Mohammed
Supporter

Languages: English (English )

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

Hello there,

Setting the feature Images programmatically requires some custom code hooked to the save_post action, The image that you are uploading through custom fields holds the image Url you might need to get the appropriate Image id so that that it can be processed using set_post_thumbnail

Assuming the custom post type as JOB and the image field as representation_image

add_action( 'save_post', 'set_cpt_featured_image_save_job', 100,3 );
  
function set_cpt_featured_image_save_job( $post_id, $post, $update ) {
// Replace with your CPT name so that it runs only on specific post type
    if ( 'job' !== $post->post_type ) { 
        return;
    }
      
    // get the image ID from the  post custom field , $_POST['wpcf-representation_image'] Gives the image url, 
    // see <em><u>hidden link</u></em>
  
 
    // set the cpt post feature image using set_post_thumbnail
    // see https://developer.wordpress.org/reference/functions/set_post_thumbnail/
}

The steps would be :
1) Use save_post hook to execute the function
2) Get the Image url from the Custom fields
3) Get the Image id check hidden link
4) use the Image id to set the post thumbnail https://developer.wordpress.org/reference/functions/set_post_thumbnail/

I hope this helps better.

Kind regards
Raja

The topic ‘[Closed] Set custom image field as custom post type's featured image via code snippet’ is closed to new replies.