Wp get all the sub pages of the parent using wp query

Here is my code

$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page','post_parent'=>$parid,'orderby'=>'title','order'=>'ASC' ));

It displays the first level sub pages only. I need all the sub page, sub’s sub page … and all. I searched for a solution and i can get all sub pages using get_pages and wp_list_pages.

But i really need to sort the order by custom post meta value. So i have to use custom query.

please help. Thanks

6

Why not just use get_pages()?

e.g.

<?php
// Determine parent page ID
$parent_page_id = ( '0' != $post->post_parent ? $post->post_parent : $post->ID );
// Get child pages as array
$page_tree_array = get_pages( array(
    'child_of' => $parent_page_id;
) );
?>

But if it really must be as a WP_Query() object, use a similar method:

<?php
// Determine parent page ID
$parent_page_id = ( '0' != $post->post_parent ? $post->post_parent : $post->ID );
// Build WP_Query() argument array
$page_tree_query_args = array(
    'post_parent' => $parent_page_id;
);
// Get child pages as a WP_Query() object
$page_tree_query = new WP_Query( $page_tree_query_args );
?>

Leave a Comment