Skip Navigation

[Resolved] Setting as featured image from post image field

This support ticket is created 4 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1390301
Screenshot_1.jpg

Hi,
I have a job site where there are two post types- Jobs and Companies and they are related.
In the Company post types, there is a custom field for images where I put specific images with the Company logo and I use the same image for displaying in the job archives (see the screenshot_1) with the help of relationship rules.
But the problem is as they are not set as post featured images they are not shown while sharing the job post on FB or sending web browser notifications.
Now my query is how to set those custom field images as post featured images? Please help.
Hope I can make you understand my problem. Thanks

#1390633

Now my query is how to set those custom field images as post featured images?
There's nothing automatic built-in to Type or Views that will use a custom field image from a one post as the featured image of another related post. I can think of a few alternatives:

1. Use an SEO plugin like Yoast SEO to set a specific image for each Job post using Facebook's OG meta tags. This is a manual process, or you could write a custom snippet variable (http://hookr.io/actions/wpseo_register_extra_replacements/). Either approach will cause the image to appear when you share the Job post on FB, but I'm not sure about web browser notifications. I don't know much about those, so I would need more input from you about how they are generated.

2. Use custom code to copy the custom field image from the Company post into the featured image of the Job post whenever the Company post is saved, or when a Toolset association is created. This is more complicated, because it requires quite a bit of custom PHP. You could set up a custom script that is triggered any time a Company post is saved using the save_post hook from WordPress: https://developer.wordpress.org/reference/hooks/save_post/
There is another hook you can use to trigger code whenever a Toolset association is created or deleted using post relationships:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_before_association_delete

You can use the Post relationships API to get the related Company post for the current Job post: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

Then you would have to write a custom script that gets the Company custom field image ID from the database, then use that ID to update the Job post's featured image: https://developer.wordpress.org/reference/functions/set_post_thumbnail/

3. If Jobs are created with Forms, you might be able to bypass the custom code and use the wpv-post-featured-image shortcode to set the featured image URL automatically. It depends on whether or not the parent Company post ID is predefined in the Job post form.

Let me know if you have questions about these solutions and I can offer more guidance.

#1392093

Hi,
I have gone through your alternatives and found that Alternative 2 would be more suitable for me. But it involves lots of code customizations. How can you help me out with this problem? Thanks.

#1392595

It's pretty complex. I will be glad to help troubleshoot any custom code you want to review together, but the majority of the code is your responsibility. If you need professional assistance, I suggest you reach out to an independent contractor. Here are some examples to get you started.

// when a job post is saved, set the related company post featured image
add_action( 'save_post', 'set_company_featured_image_save_job', 10,3 );
 
function set_company_featured_image_save_job( $post_id, $post, $update ) {
    if ( 'job' !== $post->post_type ) {
        return;
    }
     
    // get the image ID from the Job post custom field
    // see <em><u>hidden link</u></em> or <em><u>hidden link</u></em>

    // get the related company post using toolset_get_related_posts
    // see https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

    // set the related company post feature image using set_post_thumbnail
    // see https://developer.wordpress.org/reference/functions/set_post_thumbnail/
}
// when a job post and company post are related, set the company post featured image
add_action( 'toolset_association_created', 'set_company_featured_image_job_assn', 10, 5 );
function set_company_featured_image_job_assn( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
    // get the image ID from the Job post custom field
    // see <em><u>hidden link</u></em> or <em><u>hidden link</u></em>
    // set the related company post feature image using set_post_thumbnail
    // see https://developer.wordpress.org/reference/functions/set_post_thumbnail/
}
// when a job post and company post are disassociated, unset the company post featured image
add_action( 'toolset_association_created', 'unset_company_featured_image_job_dissn', 10, 5 );
function unset_company_featured_image_job_dissn( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
    set_post_thumbnail( $parent_id, 0 );
}