I need a copy of the associations between posts in another app. Is there a way to directly retrieve all associations between posts via API (e.g. toolset_get_associations) instead of querying the related posts of a specific post (toolset_get_related_posts)?
If not what way would you suggest to sync the associations?
Thanks for your help!
Hi Jonathan,
Thank you for contacting us and I'd be happy to assist.
I'm afraid, there is no single function available to get the information about all existing post associations, but as you noted, "toolset_get_related_posts" function can be used to get them on a per-post basis.
( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts )
There are a couple of ways you can approach this, based on the overall structure and requirement of your app:
1. You can create a custom function to run periodically (using Cron) that loops through one of the post types in the relationship and fetch the attached posts to them and update the existing records to keep the data in sync.
OR
2. You can use "toolset_association_created" hook ( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created ) to attach a custom function that can send the information to your external app that a new post association has been created.
Likewise "toolset_before_association_delete" hook ( ref: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_before_association_delete ) can be used to attach a custom function that can send the information to your external app that an existing post association is going to be deleted.
I hope this helps.
regard,
Waqar