WordPress at a glance

get_page_hierarchy() WP 1.0

Order the pages with children under parents in a flat list.

It uses auxiliary structure to hold parent-children relationships and runs in O(N) complexity

Works based on: _page_traverse_name()
✈ 1 time = 0.000018s = very fast | 50000 times = 0.18s = very fast PHP 7.0.8, WP 4.6

No Hooks.

Return

Array. A list arranged by hierarchy. Children immediately follow their parents.

Usage

get_page_hierarchy( &$pages, $page_id );
$pages(array) (required)
Posts array (passed by reference).
$page_id(int)
Parent page ID.
Default: 0

Code of get_page_hierarchy: wp-includes/post.php VER 4.9.8

<?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;
}

Related Functions

From category: Helper Functions

No comments
    Hello, !     Log In . Register