get_page_hierarchy
Order 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
Returns (array)
A list arranged by hierarchy. Children immediately follow their parents.
Parameters (2)
- 0. $pages (array) =>
&$pages
- Posts array, passed by reference.
- 1. $page_id — Optional. (int)
- Parent page ID. Default 0.
Usage
if ( !function_exists( 'get_page_hierarchy' ) ) { require_once ABSPATH . WPINC . '/post.php'; } // Posts array, passed by reference. $pages = array(); // Optional. Parent page ID. Default 0. $page_id = -1; // NOTICE! Understand what this does before running. $result = get_page_hierarchy($pages, $page_id);
Defined (1)
The function is defined in the following location(s).
- /wp-includes/post.php
- function get_page_hierarchy( &$pages, $page_id = 0 ) {
- if ( empty( $pages ) ) {
- return array();
- }
- $children = array();
- foreach ( (array) $pages as $p ) {
- $parent_id = intval( $p->post_parent );
- $children[ $parent_id ][] = $p;
- }
- $result = array();
- _page_traverse_name( $page_id, $children, $result );
- return $result;
- }