Make WordPress Core

Ticket #10852: new_get_page_children.diff

File new_get_page_children.diff, 1.4 KB (added by santagada, 10 years ago)

actual code, no recursion and a little faster

  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index bedf2bf..1b1d62c 100644
    function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) 
    43184318 * @return array List of page children.
    43194319 */
    43204320function get_page_children( $page_id, $pages ) {
    4321         $page_list = array();
     4321        $post_hash = array();
     4322        $children_hash = array();
     4323
     4324        // build a hash of ID -> children
    43224325        foreach ( (array) $pages as $page ) {
    4323                 if ( $page->post_parent == $page_id ) {
    4324                         $page_list[] = $page;
    4325                         if ( $children = get_page_children( $page->ID, $pages ) ) {
    4326                                 $page_list = array_merge( $page_list, $children );
    4327                         }
     4326                $post_hash[$page->ID] = $page;
     4327                if ( !array_key_exists( $page->post_parent, $children_hash ) ) {
     4328                        $children_hash[$page->post_parent] = [$page->ID];
     4329                } else {
     4330                        $children_hash[$page->post_parent][] = $page->ID;
    43284331                }
    43294332        }
    43304333
    4331         return $page_list;
     4334        if( array_key_exists( $page_id, $children_hash ) ) {
     4335                $post_list = array();
     4336                $to_look = array_reverse( $children_hash[$page_id] );
     4337                // while we still have posts to_look add them to the list
     4338                while ( $to_look ) {
     4339                        $id = array_pop( $to_look );
     4340                        $post_list[] = $post_hash[$id];
     4341                        if ( array_key_exists( $id, $children_hash ) ) {
     4342                                foreach( array_reverse( $children_hash[$id] ) as $child ) {
     4343                                        $to_look[] = $child;
     4344                                }
     4345                        }
     4346                }
     4347                return $post_list;
     4348        } else {
     4349                return array();
     4350        }
    43324351}
    43334352
    43344353/**