5

I have a hierarchical custom post type called 'products'and I have created a 2 level parent-child relationship. I want to list all the subpages/child posts on each parent post but I have tried everything and nothing works.

I tried the page-list plugin and it only works for the default 'Page' post type. Same with the tutorial found on wpbeinner.com titled "How to Display a List of Child Pages For a Parent Page in WordPress". Both work for default page type but not the custom post types.

Would appreciate some input. Thanks

1 Answer 1

6

The best way is using WP_Query. I think your error or plugin error could be that the 'post_type' of childs is not define.

WP Query : https://codex.wordpress.org/Class_Reference/WP_Query

global $post;

$args = array(
    'post_parent' => $post->ID,
    'posts_per_page' => -1,
    'post_type' => 'products', //you can use also 'any'
    );

$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
  // Do Stuff
    the_title();
    echo "<br>";
    the_content();
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
3
  • maybe you can change staus as resolved :D Commented Apr 2, 2017 at 12:30
  • 1
    I'm not sure how to mark it as resolved but I just clicked the "Answer Approval" link which by the way needs a label or something. It was not easy to find. Thanks.
    – Adam
    Commented Apr 2, 2017 at 15:30
  • thanks for highlighting can use 'any' - this was the helpful part I needed Commented Mar 17, 2020 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.