I am using the following code in a page template

    'post_parent' => 27641,
     'post_type' => 'page',
   $my_query = null;
   $my_query = new WP_Query($args);
   if( $my_query->have_posts() ) {

   while ($my_query->have_posts()) : $my_query->the_post(); ?>

to get a list of direct child pages for a product listing page.

What do I do if I want all child pages and all grandchildren too? I want to list the children of child pages as well as the direct children.

The obvious answer would be:

'post_ancestor' => 27641,


'post_grandparent' => 27641, 

but unfotunately it's not that simple.

2 Answers 2


You can use get_pages( array( 'child_of' => page_id ) ) to get all sub-pages.

  • Could you elaborate? I'm not sure what to do with that.
    – R Reveley
    Commented Oct 5, 2016 at 11:01
  • Basically this function allows you to grab all children and children of children and so on. You can find more information in WP Codex. Commented Oct 6, 2016 at 11:39

The answer by Tomasz answers the question, but for those who are Googling this, here's a little more information on get_pages().

NOTE: I would skip the WP_Query, unless you want to directly modify and alter the main query and for this example/question, you really don't need the $args

// Gets the global post variable.    
global $post;

// Get all the ancestors of page id 27641
$pages = get_pages( array( 'child_of' => 27641 ) );

// Using $post because setup_postdata requires a reference to the global $post
// Loop through each post object in $pages
foreach ( $pages as $post ) : setup_postdata( $post ); ?>
// this is your loop. Because we setup_postdata, we can use loop functions like the_title, the_content, etc.
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
// This ends the loop
    <?php endforeach; 
// Reset the postdata and continue with your page.

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.