Hello,
I'm using random numbers just for the example of the situation.
I have two posts, of two different post types, with ID "111" and "999", and a many-to-many relationship called "client-listing".
The two posts ("111" and "999") are related and their association's ID is "555".
How can I get the two post IDs ("111" and "999") if I only have the ID of their association ("555")?
Thank you.
Hi,
Thank you for contacting us and I'd be happy to assist.
If you know the ID of any of the post elements from a relationship, for example, an intermediary/association post, you can get the other related elements, using the "toolset_get_related_posts" function:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
For example, to get the parent and child posts from an intermediary/association post with ID '555', the code will look like this:
$target_association_id = '555';
$get_results = toolset_get_related_posts( $target_association_id, 'recipe-company', 'intermediary', 1, 0, array(), 'post_id', array('parent','child'));
if(!empty($get_results)) {
$parent_post_id = $get_results[0]['parent'];
$child_post_id = $get_results[0]['child'];
}
As a result, you'll have the IDs of the related parent and child posts, in the variables '$parent_post_id ' and '$child_post_id', respectively.
I hope this helps and please let me know if you need any further assistance with this.
regards,
Waqar
This is exactly what I was looking for. Thank you very much!