WordPress.org

Make WordPress Core

Ticket #10852: new_get_page_children.2.diff

File new_get_page_children.2.diff, 1.6 KB (added by santagada, 5 years ago)

new version of the patch

  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index bedf2bf..f124b41 100644
    function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) 
    43114311 * children for the same to retrieve all children of a page. Does not make any
    43124312 * SQL queries to get the children.
    43134313 *
     4314 * It uses auxiliary structure to hold parent-children relationships and
     4315 * runs in O(N) complexity
     4316 *
    43144317 * @since 1.5.1
    43154318 *
    43164319 * @param int   $page_id    Page ID.
    function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) 
    43184321 * @return array List of page children.
    43194322 */
    43204323function get_page_children( $page_id, $pages ) {
    4321         $page_list = array();
    4322         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 );
     4324        // build a hash of ID -> children
     4325        $children = array();
     4326        foreach ( (array) $pages as $p ) {
     4327                $children[ intval( $p->post_parent ) ][] = $p;
     4328        }
     4329
     4330        $post_list = array();
     4331        if( array_key_exists( $page_id, $children ) ) {
     4332                $to_look = array_reverse( $children[ $page_id ] );
     4333                // while we still have posts to_look add them to the list
     4334                while ( $to_look ) {
     4335                        $p = array_pop( $to_look );
     4336                        $post_list[] = $p;
     4337                        if ( array_key_exists( $p->ID, $children ) ) {
     4338                                foreach ( array_reverse( $children[ $p->ID ] ) as $child ) {
     4339                                        $to_look[] = $child;
     4340                                }
    43274341                        }
    43284342                }
    43294343        }
    43304344
    4331         return $page_list;
     4345        return $post_list;
    43324346}
    43334347
    43344348/**