Make WordPress Core


Ignore:
Timestamp:
05/11/2010 02:21:03 PM (16 years ago)
Author:
ryan
Message:

Reduce number of queries in wp_get_nav_menu_items() by fetching posts and terms with batch queries. see #12734

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/nav-menu.php

    r14528 r14557  
    413413        $items = get_objects_in_term( $menu->term_id, 'nav_menu' );
    414414
    415         if ( ! empty( $items ) ) {
    416                 $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true,
    417                                                   'update_post_term_cache' => false);
    418                 $args = wp_parse_args( $args, $defaults );
    419                 if ( count( $items ) > 1 )
    420                         $args['include'] = implode( ',', $items );
    421                 else
    422                         $args['include'] = $items[0];
    423 
    424                 $items = get_posts( $args );
    425 
    426                 if ( is_wp_error( $items ) || ! is_array( $items ) ) {
    427                         return false;
    428                 }
    429 
    430                 $items = array_map( 'wp_setup_nav_menu_item', $items );
    431 
    432                 if ( ARRAY_A == $args['output'] ) {
    433                         $GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
    434                         usort($items, '_sort_nav_menu_items');
    435                         $i = 1;
    436                         foreach( $items as $k => $item ) {
    437                                 $items[$k]->$args['output_key'] = $i++;
    438                         }
    439                 }
    440         }
     415        if ( empty( $items ) )
     416                return $items;
     417
     418        $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true,
     419                                          'update_post_term_cache' => false);
     420        $args = wp_parse_args( $args, $defaults );
     421        if ( count( $items ) > 1 )
     422                $args['include'] = implode( ',', $items );
     423        else
     424                $args['include'] = $items[0];
     425
     426        $items = get_posts( $args );
     427
     428        if ( is_wp_error( $items ) || ! is_array( $items ) )
     429                return false;
     430
     431        // Get all posts and terms at once to prime the caches
     432        $posts = array();
     433        $terms = array();
     434        foreach ( $items as $item ) {
     435                $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true );
     436                $object = get_post_meta( $item->ID, '_menu_item_object', true );
     437                $type = get_post_meta( $item->ID, '_menu_item_type', true );
     438
     439                if ( 'post_type' == $type )
     440                        $posts[$object][] = $object_id;
     441                elseif ( 'taxonomy' == $type)
     442                        $terms[$object][] = $object_id;
     443        }
     444
     445        if ( !empty($posts) ) {
     446                foreach ( array_keys($posts) as $post_type ) {
     447                        get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) );
     448                }
     449        }
     450        unset($posts);
     451
     452        if ( !empty($terms) ) {
     453                foreach ( array_keys($terms) as $taxonomy ) {
     454                        get_terms($taxonomy, array('include' => $terms[$taxonomy]) );
     455                }
     456        }
     457        unset($terms);
     458
     459        $items = array_map( 'wp_setup_nav_menu_item', $items );
     460
     461        if ( ARRAY_A == $args['output'] ) {
     462                $GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
     463                usort($items, '_sort_nav_menu_items');
     464                $i = 1;
     465                foreach( $items as $k => $item ) {
     466                        $items[$k]->$args['output_key'] = $i++;
     467                }
     468        }
     469
    441470        return $items;
    442471}
Note: See TracChangeset for help on using the changeset viewer.