Title: Post type tree loop
Last modified: August 22, 2016

---

# Post type tree loop

 *  [NelutuO](https://wordpress.org/support/users/nelutuo/)
 * (@nelutuo)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/post-type-tree-loop/)
 * Hi.
 * I need to get the pages of a custom post type exactly as they are in the admin
   panel.
    Example:
 * Page1
    –subpage1 –subpage2 Page2 Page3 –subpage1 –subpage2
 * I need this as a loop.
 * I tried for a few days to do this but no success.
 * Can anyone help me? Thank you. Neluțu.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Rastislav Lamos](https://wordpress.org/support/users/lamosty/)
 * (@lamosty)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/post-type-tree-loop/#post-5931821)
 * Hi NelutuO.
 * Do you have any code to show?
 *  Thread Starter [NelutuO](https://wordpress.org/support/users/nelutuo/)
 * (@nelutuo)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/post-type-tree-loop/#post-5931863)
 * I’m a beginner so what I have is very basic and may not help to much.
    I have
   a basic loop and two variables that I thought may hold the key.
 *     ```
       <?php $loop= new WP_Query( array( 'post_type'=> 'post-type', 'posts_per_page' => -1 ) ); ?>
       <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
           <?php echo $id = get_the_ID(); ?>
            -
           <?php echo $parent = $post->post_parent; ?>
   
           <?php the_title(); echo "<br />"; ?>
   
       <?php endwhile; wp_reset_query(); ?>
       ```
   
 * What I was trying to do but didn’t managed is this:
 * display the pages that have the parent equal to zero
    check if the page is a 
   parent if it is -> loop and display the child pages
 * I don’t know if WP_Query can display the pages that have a specific parent (wich
   we know since we have its id) and I don’t know if a WP_Query can be used inside
   another WP_Query
 *  [PhPCentre](https://wordpress.org/support/users/phpcentre/)
 * (@phpcentre)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/post-type-tree-loop/#post-5931882)
 * Use this:
 *     ```
       $loop = new WP_Query( array( 'post_type' => 'your_post_type', 'post_parent' => 0 , 'posts_per_page' => -1 ) );
       while ( $loop->have_posts() ) : $loop->the_post();
           //parent page title
           the_title();
           $parent_page_id = $post->ID;
           $child_pages = get_pages( array( 'child_of' => $parent_page_id , 'post_type' => 'your_post_type', 'posts_per_page' => -1 ) );
           if ( $child_pages != false ) {
               foreach( $child_pages as $page ) {
                    //child page title
                   echo  $page->post_title;
               }
            }
       endwhile;
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Post type tree loop’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 3 replies
 * 3 participants
 * Last reply from: [PhPCentre](https://wordpress.org/support/users/phpcentre/)
 * Last activity: [11 years, 1 month ago](https://wordpress.org/support/topic/post-type-tree-loop/#post-5931882)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
