Skip Navigation

[Resolved] Showing related children of parent on current child post

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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: Asia/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by Minesh 1 year, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2465069

Hi,

I have 2 cpt's that are in Toolset relationship.

"artist" is the parent post and "concord-albums" is the child.

I want to build a WP query string that can pull other related children of the current child post and list those at bottom of current post while excluding the current post.

For example I am looking at an album by an artist and I want to see all other albums by that artist at bottom of current post but exclude the current post.

How do I go about doing this?

Thanks

#2465509

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

We offer post relationship API functions that should help you to query the related posts:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/

If you want WP_Query then you should check the following doc:
- https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

With the following Doc you will get all information how you can query parent/child posts using PHP:
- https://toolset.com/documentation/customizing-sites-using-php/

Please let me know if you require further help.

#2466901

Thanks so using the example you sent. If I have a cpt called "artist" (parent) and a cpt called "concord-albums" (child) and I want to show other children of same parent on a child cpt (so looking at one album, I want all related albums to parent of current child listed at bottom of current post)but want to exclude the current post it would look something like this?:

$query = new WP_Query( 
 array(
  'post_type' => 'concord-albums,
  'posts_per_page' => -1,
  //new toolset_relationships query argument
  'toolset_relationships' => array(
   'role' => 'child',
   'related_to' => get_the_ID(), WHAT GOES HERE? 'artist'?
   // this will work only with relationships that have existed before the migration
   // if possible, use the relationship slug instead of an array
   'relationship' => array( 'artist, 'concord-albums' )
  ),
  'order' => 'ASC',
 )
);
$posts = $query->posts;
#2467007

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - so you will have to use the following way:

global $post;
  
 $post_relationship_slug = 'personal-trainer-session';  // change the post relationship slug to your original post relationship slug
 $parent_id = toolset_get_related_post($post->ID,$post_relationship_slug);   // getting parent_id based on the current child post
  
  $query = new WP_Query( 
 array(
  'post_type' => 'concord-albums',
  'posts_per_page' => -1,
  'toolset_relationships' => array(
   'role' => 'child',
   'related_to' =>  $parent_id,
   'relationship' =>  $post_relationship_slug,   // slug of the post relationship
  ),
  'order' => 'ASC',
  'post__not_in' => array($post->ID),  // excluding the current post
 )
);
$found_posts = $query->posts;
  
  echo "<pre>";
  print_r($found_posts );
  exit;

With $found_posts you will see the desired post array. You should loop through the $found_posts and display your posts as required.

#2467837

Thank you this is great. I now am wondering how I can pull through the "Artist Name" which would be the $parent_id in this case. I am able to return the albums correctly but how can I grab the related artist name to show on front. I am not using views so that is not an option.

Thanks

#2468941

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

If the artist name is parent then here is the Doc about how you can query the parent:
- https://toolset.com/documentation/customizing-sites-using-php/displaying-parent-posts/

If artist name is child then here is the Doc about how you can query child posts:
- https://toolset.com/documentation/customizing-sites-using-php/displaying-child-posts/

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.