Query in a Hierarchical Custom Post Type for Children vs Siblings

I have a custom post type with two levels of pages inside of it. the parent level we’ll call parent, and child – child.

The parents need to be able to pull in their children, and the children need to be able to pull in their siblings (children of the same parent.)

If i were doing this in a normal page:

<?php $children = get_pages('child_of=".$post->ID);?>
if( count( $children ) != 0 ) { ?>
<section class="children">
Give us your Children
</section>
<?php }
else { ?>
<section class="siblings">
Give us your siblings
</section>
<?php } ?>

Of course – this doesn”t work since get_pages only works with non-cpt “pages”. I’m at a loss for the proper method inside a CPT – help much appreciated.

1 Answer
1

get_pages() works with other post types just fine, you only need to pass post_type you want as part of argument (also I recommend to stick with array notation).

$children = get_pages( array( 'child_of' => $post->ID, 'post_type' => 'your_post_type', ) );

Leave a Comment