Skip Navigation

[Resolved] Saving image field as featured image

This support ticket is created 3 years, 9 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 14 replies, has 2 voices.

Last updated by Shane 3 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#1691819

I am using two types of custom posts- Company and Job and they are related in one to many way format.
I use one image custom field in Company and display the same in related job posts too. I want to use this 'image of company' as a 'featured image of the job'. I am closely following instructions given in this solution (used for another purpose) provided at https://toolset.com/forums/topic/affect-an-image-field-as-featured-image-for-a-product/
But I couldn't make out the same though I have fetched the image.
For the purpose I am sharing two URLs to see the things:
hidden link (job post)
hidden link (company post)
Please help me to get the desired result. Thanks

#1692151

Shane
Supporter

Languages: English (English )

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

Hello,

Thank you for getting in touch.

Are you currently creating the child post from the frontend ?

Secondly you may not need to actually store the image on the child post itself as you should be able to call for the Parent image in the child post.

Can you let me know how you are building the template for the child post. Is it with our Toolset blocks plugin and the gutenburg editor ?

Please let me know.
Thanks,
Shane

#1692447

Hi,
I am creating the child in the backend.
I am using Toolset Types and Toolset views plugin to generate the results.
For better clarification, you may have access to the admin page.

#1692953

Shane
Supporter

Languages: English (English )

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

Hi Deep,

You can provide the admin access in the private reply that I will be enabling.

What I'm saying is that instead of copying over the Parent's featured image, you can load the parent's Featured image directly from the child template because we will have access to the parent's data.

Please let me know what you think of this solution, as copying over the parent post type image will require the use of custom coding.

Thanks,
Shane

#1693073

Shane
Supporter

Languages: English (English )

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

Hi Deep,

I've edited the content template to include this line here
[types field='company-image' title='%%TITLE%%' alt='%%ALT%%' size='full' item='@company-job.parent'][/types]

This will pull the parent image of the current job. Secondly I can understand why you would want to do this but it you will need to go through and update the posts one by one.

You can use a hook to copy over parent image field to the featured image field, however when you are doing this you will need to go through each post one by one and hit the update button, which is just a little faster than manually adding the images.

Thanks,
Shane

#1693151

I understand your point.
You can use a hook to copy over parent image field to the featured image field, however, when you are doing this you will need to go through each post one by one and hit the update button, which is just a little faster than manually adding the images.
But how to create a hook. Please help.

#1694513

Shane
Supporter

Languages: English (English )

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

Hello,

I thought of an easy way to do this for you. We can make use of our views plugin to loop through the posts and automatically update them for us.

I've added the following shortcode to your custom code section.

// Add Shortcode
function custom_shortcode() {
	$child = get_the_ID();
	$parent = toolset_get_related_post( $child, "company-job" );
	$featured = get_post_meta($parent,'wpcf-company-image');
    set_post_thumbnail($child,attachment_url_to_postid($featured[0]));
}
add_shortcode( 'set_child_featured_Image', 'custom_shortcode' );

Create a view that is listing out all the jobs and add the following to the loop section of the view [set_child_featured_Image]

Then add the view to a page and visit that view. It should loop through and automatically update all the posts with their respective parent featured image.

Please let me know if this helps.
Thanks,
Shane

#1697727

Hi Shane,
Thanks for your support. As I have seen you have already created the custom code 'test' which I have inserted in a newly created view called 'Job Image View' (ID 31629).
Then I placed the view in the job template (single) in line no. 5. Now?
The images are not showing up either in the post or while sharing on Facebook. Or maybe I am getting what you are suggesting.

#1701387

Shane
Supporter

Languages: English (English )

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

Hello,

Were you able to resolve the issue where the images weren't showing up on the Posts? From what I can see here in the link below they are now showing up.
hidden link

If you are still having issues with this please let me know.

Secondly with the sharing of the post to facebook you will need to open a second ticket for this, however try this below.
hidden link

Thanks,
Shane

#1702597
Screenshot_1.jpg

I don't understand whats the use of the above shortcode because image was pulled by the following code

[types field='company-image' title='%%TITLE%%' alt='%%ALT%%' size='full' item='@company-job.parent'][/types]

rather than the

[wpv-view name="job-image-view"]

Please see the job template. Hope that would clarify the issue.

#1702931

Shane
Supporter

Languages: English (English )

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

Hello,

Ok so let me explain in a bit more details.

This view here

[wpv-view name="job-image-view"]

It was created for the sole purpose to make use of this shortcode in my previous post.
https://toolset.com/forums/topic/saving-image-field-as-featured-image/#post-1694513

What it's meant to do is to loop through every Job and copy the Parent's image into the child Featured image section.

Secondly this shortcode below.

[types field='company-image' title='%%TITLE%%' alt='%%ALT%%' size='full' item='@company-job.parent'][/types]

This is meant to pull and display the parent image on the child post.

Given that we have copied over the images to the child post then you no longer need this specific shortcode. What you need to do now is to change it to [wpv-post-featured-image] and your images should display from the Job post itself.

Please let me know if this clears things up for you.

Thanks,
Shane

#1703129

Well Shane, what I am able to understand that we don't need that custom shortcode created by you as it has copied the entire images of the parent post in child post. Hence I deactivated it and pasted the [wpv-post-featured-image] in the child post which is perfectly fetching the parent image as the featured image of the child post.
Will it work for future posts even if I keep it deactivated?

Now for the second part i.e. sharing the same child featured image in FB and Twitter you are suggesting to open a new ticket. If you resolve the same for me I'd be immensely happy. Meanwhile, I have gone through your suggested link. It's suggesting about Yoast which I am already using (but doesn't help much as I have to choose every image for the post). What I want is to automatically add the featured image (with OG tag) while posting Facebook/Twitter.
Here I read a solution but couldn't help. https://wordpress.stackexchange.com/questions/159162/set-custom-post-feature-image-as-ogimage

#1703135

Shane
Supporter

Languages: English (English )

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

Hello,

Given that we've copied over all the existing image, we can then convert this shortcode to a hook that will fire each time a new child post is created.


 
function set_child_featured_image($post_id) {
    // If this is a revision, get real post ID
    if ( get_post_type($post_id) == 'job' ) 
        $child = $post_id;
      $parent = toolset_get_related_post( $child, "company-job" );
      $featured = get_post_meta($parent,'wpcf-company-image');
       set_post_thumbnail($child,attachment_url_to_postid($featured[0]));
    }
}
add_action( 'save_post', 'set_child_featured_image' );

Please add this to your custom functions and activate it. Ensure that the Backend option is checked when you are adding the code to the Toolset custom code section.

Given that the Facebook issue is separate from this one then you will need to open a new ticket. It helps to keep our forums organised and assist other users who are experiencing a similar issue. I hope you can understand this.

Thanks,
Shane

#1703149

The code is showing some error. Saved as featured-company-image in custom code. Please check.

#1703211

Shane
Supporter

Languages: English (English )

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

Hello,

This should now be resolved, the issue was with a missing bracket.

Thanks,
Shane

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