On this page
get_page_hierarchy( WP_Post[] $pages, int $page_id ): string[]
Orders the pages with children under parents in a flat list.
Description
It uses auxiliary structure to hold parent-children relationships and runs in O(N) complexity
Parameters
$pagesWP_Post[] Required-
Posts array (passed by reference).
$page_idint Optional-
Parent page ID. Default 0.
Return
string[] Array of post names keyed by ID and arranged by hierarchy. Children immediately follow their parents.
Source
File: wp-includes/post.php. View all references
function get_page_hierarchy( &$pages, $page_id = 0 ) {
if ( empty( $pages ) ) {
return array();
}
$children = array();
foreach ( (array) $pages as $p ) {
$parent_id = (int) $p->post_parent;
$children[ $parent_id ][] = $p;
}
$result = array();
_page_traverse_name( $page_id, $children, $result );
return $result;
}
Related
Uses
| Uses | Description |
|---|---|
| _page_traverse_name() wp-includes/post.php | Traverses and return all the nested children post names of a root page. |
Used By
| Used By | Description |
|---|---|
| WP_Rewrite::page_uri_index() wp-includes/class-wp-rewrite.php | Retrieves all pages and attachments for pages URIs. |
Changelog
| Version | Description |
|---|---|
| 2.0.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_page_hierarchy