Hello,
I'm currently setting up your new post relationships feature on my site - and I am looking through your documentation (here: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/). I am trying to write a PHP function using the toolset_get_related_post function to get a related post - when I have the intermediary post type object.
Let me explain the structure of my posts:
I have a post type: Article which is in a many to many relationship with a post type Place
I have queried the intermediary post type object (Let's call it Pipe)
So given a Pipe post object (with a specific value to some intermediary post field) - how might I acquire the ID of the associated Place post?
I am a little bit confused by the documentation of the toolset_get_related_post function - and would prefer it if I could simply grab the relevent post IDs from the Pipe post meta field. If you could just let me know how the related posts are stored in the intermediary post types - that would also work perfectly for me!
Thank you!
Hello,
I ended up solving the problem using the toolset_get_related_posts (plural) function - however I had to fill in each field in order to get it to work. It would be nice if there were some non required fields. I also wasn't quite sure which post type was the parent and which was the child in the relationship. Obviously that was easy to figure out after a bit of fiddling since there are only two options.
Is there a simpler way to do this?
I am a little bit confused by the documentation of the toolset_get_related_post function - and would prefer it if I could simply grab the relevent post IDs from the Pipe post meta field
Actually in the new relationships system, post relationships are no longer maintained directly in the postmeta table. Types relationships are stored in some Toolset-specific tables instead. The toolset_get_related_post API is effective in some simpler cases, but when an intermediary post is used as the query reference it's not useful. The toolset_get_related_posts API is the most effective way to get the related posts using the intermediary post ID in PHP. Here's another ticket that discusses a similar request: https://toolset.com/forums/topic/get-parent-id-from-intermediary-using-php/
You can determine which post type is parent and which post type is child by going to Toolset > Relationships. Edit the M2M relationship and toggle open the Settings panel. Check "I understand" and click Edit Settings. It is not advised to make changes here, but you can see which post type is listed as parent and which is listed as child.
Regarding the complexity of the toolset_get_related_posts function, I think it would be a great suggestion to extend the toolset_get_related_post (singular) function to work with an intermediary post ID. An intermediary post can only connect two posts, one is a parent and one is a child. So the simpler function would be useful here. I encourage you to submit a new ticket in the forum using the "Suggest an improvement" option. Recommend extending the simple toolset_get_related_post function to work with intermediary post IDs as a simpler alternative to the toolset_get_related_posts function. Our developers will analyze the request and let you know if it's something they can implement. My guess is that the toolset_get_related_posts function will remain complex in order to support more robust queries, but these APIs are still evolving.