get_page_hierarchy( array $pages, int $page_id )

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

Description Description

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

Parameters Parameters


(array) (Required) Posts array (passed by reference).


(int) (Optional) Parent page ID. Default 0.

Top ↑

Return Return

(array) A list arranged by hierarchy. Children immediately follow their parents.

Top ↑

Source Source

File: 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;

Top ↑

Changelog Changelog

Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.