Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

<?php
    $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
add comment

1 Answer 1

Placing the following code within the WP_Query loop works:

<?php
                $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
                        continue;

                    $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
add comment

Your Answer

 
discard

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.