get_page_children()

You are here:

get_page_children( int $page_idarray $pages )

Identify descendants of a given page ID in a list of page objects.

Description Description

Descendants are identified from the $pages array passed to the function. No database queries are performed.


Top ↑

Parameters Parameters

$page_id

(int) (Required) Page ID.

$pages

(array) (Required) List of page objects from which descendants should be identified.


Top ↑

Return Return

(array) List of page children.


Top ↑

More Information More Information

This function calls itself recursively.


Top ↑

Source Source

File: wp-includes/post.php

5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
function get_page_children( $page_id, $pages ) {
    // Build a hash of ID -> children.
    $children = array();
    foreach ( (array) $pages as $page ) {
        $children[ intval( $page->post_parent ) ][] = $page;
    }
 
    $page_list = array();
 
    // Start the search by looking at immediate children.
    if ( isset( $children[ $page_id ] ) ) {
        // Always start at the end of the stack in order to preserve original `$pages` order.
        $to_look = array_reverse( $children[ $page_id ] );
 
        while ( $to_look ) {
            $p           = array_pop( $to_look );
            $page_list[] = $p;
            if ( isset( $children[ $p->ID ] ) ) {
                foreach ( array_reverse( $children[ $p->ID ] ) as $child ) {
                    // Append to the `$to_look` stack to descend the tree.
                    $to_look[] = $child;
                }
            }
        }
    }
 
    return $page_list;
}


Top ↑

Changelog Changelog

Changelog
Version Description
1.5.1 Introduced.
Was this article helpful?
Dislike 0
Views: 5