Skip Navigation

[Resolved] Retrieve all child posts for a specific parent in relationship

This support ticket is created 3 years, 9 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: Africa/Casablanca (GMT+01:00)

This topic contains 2 replies, has 2 voices.

Last updated by parisK 3 years, 9 months ago.

Assisted by: Jamal.

Author
Posts
#2237335

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

#2237489

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.

#2238251

My issue is resolved now. Thank you!