HTTP/2 301
server: nginx
date: Tue, 14 Oct 2025 05:53:00 GMT
content-type: text/html; charset=UTF-8
location: https://developer.wordpress.org/reference/functions/get_page_hierarchy/
x-content-type-options: nosniff
content-language: en
x-ua-compatible: IE=Edge
vary: Accept-Encoding, Cookie
expires: Thu, 01 Jan 1970 00:00:00 GMT
cache-control: no-cache, no-store, max-age=0, must-revalidate
pragma: no-cache
alt-svc: h3=":443"; ma=86400
strict-transport-security: max-age=31536000
HTTP/2 200
server: nginx
date: Tue, 14 Oct 2025 05:53:02 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
x-olaf: ⛄
vary: accept, content-type
link:
; rel="https://api.w.org/"
link:
; rel="alternate"; title="JSON"; type="application/json"
link: ; rel=shortlink
x-frame-options: SAMEORIGIN
content-encoding: gzip
alt-svc: h3=":443"; ma=86400
x-nc: MISS ord 2
get_page_hierarchy() – Function | Developer.WordPress.org
get_page_hierarchy( WP_Post[] $pages, int $page_id ): string[]
Orders 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
$pages
WP_Post[]requiredPosts array (passed by reference).
$page_id
intoptionalParent page ID. Default 0.
string[] Array of post names keyed by ID and arranged by hierarchy. Children immediately follow their parents.
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;
}
View all references View on Trac View on GitHub
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.