get_page_children

The timeline below displays how wordpress function get_page_children has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Identifies descendants of a given page ID in a list of page objects.
 *
 * Descendants are identified from the `$pages` array passed to the function. No database queries are performed.
 *
 * @since 1.5.1
 *
 * @param int       $page_id Page ID.
 * @param WP_Post[] $pages   List of page objects from which descendants should be identified.
 * @return WP_Post[] List of page children.
 */
function get_page_children($page_id, $pages)
{
    // Build a hash of ID -> children.
    $children = array();
    foreach ((array) $pages as $page) {
        $children[(int) $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;
}

WordPress Version: 5.6

/**
 * Identify descendants of a given page ID in a list of page objects.
 *
 * Descendants are identified from the `$pages` array passed to the function. No database queries are performed.
 *
 * @since 1.5.1
 *
 * @param int   $page_id Page ID.
 * @param array $pages   List of page objects from which descendants should be identified.
 * @return array List of page children.
 */
function get_page_children($page_id, $pages)
{
    // Build a hash of ID -> children.
    $children = array();
    foreach ((array) $pages as $page) {
        $children[(int) $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;
}

WordPress Version: 4.3

/**
 * Identify descendants of a given page ID in a list of page objects.
 *
 * Descendants are identified from the `$pages` array passed to the function. No database queries are performed.
 *
 * @since 1.5.1
 *
 * @param int   $page_id Page ID.
 * @param array $pages   List of page objects from which descendants should be identified.
 * @return array List of page children.
 */
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;
}

WordPress Version: 4.1

/**
 * Retrieve child pages from list of pages matching page ID.
 *
 * Matches against the pages parameter against the page ID. Also matches all
 * children for the same to retrieve all children of a page. Does not make any
 * SQL queries to get the children.
 *
 * @since 1.5.1
 *
 * @param int   $page_id    Page ID.
 * @param array $pages      List of pages' objects.
 * @return array List of page children.
 */
function get_page_children($page_id, $pages)
{
    $page_list = array();
    foreach ((array) $pages as $page) {
        if ($page->post_parent == $page_id) {
            $page_list[] = $page;
            if ($children = get_page_children($page->ID, $pages)) {
                $page_list = array_merge($page_list, $children);
            }
        }
    }
    return $page_list;
}

WordPress Version: 4.0

/**
 * Retrieve child pages from list of pages matching page ID.
 *
 * Matches against the pages parameter against the page ID. Also matches all
 * children for the same to retrieve all children of a page. Does not make any
 * SQL queries to get the children.
 *
 * @since 1.5.1
 *
 * @param int   $page_id Page ID.
 * @param array $pages   List of pages' objects.
 * @return array List of page children.
 */
function get_page_children($page_id, $pages)
{
    $page_list = array();
    foreach ((array) $pages as $page) {
        if ($page->post_parent == $page_id) {
            $page_list[] = $page;
            if ($children = get_page_children($page->ID, $pages)) {
                $page_list = array_merge($page_list, $children);
            }
        }
    }
    return $page_list;
}

WordPress Version: 3.7

/**
 * Retrieve child pages from list of pages matching page ID.
 *
 * Matches against the pages parameter against the page ID. Also matches all
 * children for the same to retrieve all children of a page. Does not make any
 * SQL queries to get the children.
 *
 * @since 1.5.1
 *
 * @param int $page_id Page ID.
 * @param array $pages List of pages' objects.
 * @return array
 */
function get_page_children($page_id, $pages)
{
    $page_list = array();
    foreach ((array) $pages as $page) {
        if ($page->post_parent == $page_id) {
            $page_list[] = $page;
            if ($children = get_page_children($page->ID, $pages)) {
                $page_list = array_merge($page_list, $children);
            }
        }
    }
    return $page_list;
}