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
$pages
WP_Post[]required- Posts array (passed by reference).
$page_id
intoptional- Parent page ID. Default 0.
Source
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;
}
Changelog
Version | Description |
---|---|
2.0.0 | Introduced. |
get_page_hierarchy() returns an ID=>page_title array: the Key is ID of a Page, and the Value is the Page’s Title.
The Title is not modified by indentation or other means to indicate a Child Page immediately below its Parent Page.