Skip Navigation

[Resolved] Attach Featured Image of a Related Post

This support ticket is created 5 years, 6 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 1 reply, has 2 voices.

Last updated by Waqar 5 years, 6 months ago.

Assisted by: Waqar.

Author
Posts
#1275407

Can you help how to attach a featured image in the current post from the related post?

#1275449

Hi there,

Thank you for contacting us and I'll be happy to assist.

From your message, I understand that you'd like to attach the featured image from the related post when a new post is created or updated from the admin area.

If this is correct, you'll need a custom function for this, since this feature is not available, out-of-the-box.

As much as we would like to help, 1-1 code customization is beyond the scope of support that we provide. But, we do our best to guide in the right direction, whenever possible.

To execute a custom function for this, when a post is created or updated from the admin area, you can use the hook "save_post":
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

To get the ID of the related post, you can use the Toolset function "toolset_get_related_posts":
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

To get the ID of the featured image from that related post, you can use the function "get_post_thumbnail_id":
https://codex.wordpress.org/Function_Reference/get_post_thumbnail_id

As the last step, to set that same image as the featured image of the current post, you can use the function "set_post_thumbnail":
https://developer.wordpress.org/reference/functions/set_post_thumbnail/

Note: You'll find a good example of how these functions can be used together in this reply:
https://stackoverflow.com/a/49126804

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar