WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am currently using WP_Query to loop through a custom post type, and only showing the parent posts like so:

    $loop = new WP_Query( array(
        'post_type' => 'programs',
        'posts_per_page' => -1,
        'post_parent' => 0
    ) );
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

    loop stuff

<?php endwhile; ?>

Within each loop, I need to to do another loop, which shows all of the children of that post that is currently being iterated on.

The point is to show each post, and then all of the children of that post underneath it. I need to loop through the children pages so I can show the title, content, image, custom fields, etc. I do not know how to approach this, as using a QP_Query loop inside another leads to unexpected results. Any ideas?

share|improve this question

Placing the following code within the WP_Query loop works:

                $parent_page_id = ( '0' != $post->post_parent ? $post->post_parent : $post->ID ); 
                $mypages = get_pages( array( 'child_of' => $parent_page_id,  'post_type' => 'programs' ) );

                foreach( $mypages as $page ) {      
                    $content = $page->post_content;
                    if ( ! $content ) // Check for empty page

                    $content = apply_filters( 'the_content', $content );

                <h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
                <div class="entry"><?php echo $content; ?></div>

                <?php }  ?>

This solution is taken from: http://codex.wordpress.org/Function_Reference/get_pages

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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