1

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?

1 Answer 1

2

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.