2

I need to list subpages, given the id of parent one. I tried as follows:

if ($children = get_pages(array('child_of' => $post->ID)) && !empty($children)) {
    //Do stuff here
}

But this returns false anyways.

I tried as well get_page_children($post->ID, array), but I think I didn't really get how this function works.

So, is it possible at all to get one page's children? Thank you.

  • 1
    I think you have a problem in your if-sentence, I don't think you can assign a value to the $children variable and check if it's non-empty within the same if-condition. Try to move the variable assigning part out of it. Test e.g. this code: if( $x = 1 && ! empty( $x ) ) { echo 'passed';} and check if you get `passed' output. – birgire May 29 '17 at 20:23
  • There's also the wp_list_pages() if you need to list subpages of a given page.. – birgire May 29 '17 at 20:35
2

The example from the WP codex for get_page_children does what you are looking for with a page titled "Portfolio":

$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => '-1'));

// Get the page as an Object
$portfolio =  get_page_by_title('Portfolio');

// Filter through all pages and find Portfolio's children
$portfolio_children = get_page_children( $portfolio->ID, $all_wp_pages );

The first part initializes a new WP_Query, then using this is it is querying all pages and returning them as objects set to the $all_wp_pages variable.

get_page_children needs two arguments: the ID of the parent page, and the list of pages' objects in which to look for that page's children (the $all_wp_pages variable above).

For your example to work, you need that list of objects created by the query ($all_wp_pages) as the get_page_children doesn't query the DB, but rather checks against that list.

So, get_page_children($post->ID, $all_wp_pages)

In full:

$page_id = $post->ID;
$my_wp_query = new WP_Query();

$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => '-1'));

$the_pages_children = get_page_children( $page_id, $all_wp_pages );
  • Still returning empty... – Daniele Squalo May 29 '17 at 20:46
  • what is returning empty? have you checked the value of $all_wp_pages just before calling get_page_children()? Checked $page_id? Either of those off? – hwl May 29 '17 at 20:53
  • $all_wp_pages returns the full list of posts of page type; $page_id is correct, but get_page_children is returning as empty array – Daniele Squalo May 29 '17 at 21:00
  • that would be saying there are no children for the page id... the function itself is pretty straightforward, not sure how it could break... not saying it couldn't, just don't know why if the id has children it would say they don't. you can see it here: core.trac.wordpress.org/browser/tags/4.7.3/src/wp-includes/… – hwl May 29 '17 at 21:11
  • Somehow it seems i was getting post_type's id instead of post id itself. I managed to get my var as needed, but maybe i was trying to solve the wrong problem. – Daniele Squalo May 29 '17 at 21:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.