Tell us what you are trying to do?
Hi! I have 2 CPTs, named "candidate-profiles" and "job-openings" and I have created a many-to-many relationship named "couples" since a candidate-profile might fit one or more job-openings and one job-opening might fit one or more candidate-profiles.
I'm setting up a PHP snippet where I want a) given the ID of a candidate-profile (parent) to retrieve all job-openings (children) and b) given the ID of a job-opening (child) to retrieve all candidate-profiles (parents). These two are two different cases.
Is there any documentation that you are following?
Yes, I tried this one https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/ but I was quite confused.
Thank you
Hello and thank you for contacting the Toolset support.
Assuming that the relationship slug is "candidate-profiles-job-openings", you can get the child posts like this:
$children = toolset_get_related_posts( $parent_id, "candidate-profiles-job-openings", array(
'query_by_role' => 'parent',
'role_to_return' => 'child',
) );
And you can get the parent posts like this:
$children = toolset_get_related_posts( $child_id, "candidate-profiles-job-openings", array(
'query_by_role' => 'child',
'role_to_return' => 'parent',
) );
The following codes will return an array of post IDs, you can use the option "return" to get an array of objects:
$children = toolset_get_related_posts( $parent_id, "candidate-profiles-job-openings", array(
'query_by_role' => 'parent',
'role_to_return' => 'child',
'return' => 'post_object',
) );
I hope this helps. Let me know if you have any questions.
My issue is resolved now. Thank you!