diff --git a/wp-includes/nav-menu.php b/wp-includes/nav-menu.php index 68ca725a73..f0a3909023 100644 --- a/wp-includes/nav-menu.php +++ b/wp-includes/nav-menu.php @@ -412,32 +412,61 @@ function wp_get_nav_menu_items( $menu, $args = array() ) { $items = get_objects_in_term( $menu->term_id, 'nav_menu' ); - if ( ! empty( $items ) ) { - $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true, - 'update_post_term_cache' => false); - $args = wp_parse_args( $args, $defaults ); - if ( count( $items ) > 1 ) - $args['include'] = implode( ',', $items ); - else - $args['include'] = $items[0]; + if ( empty( $items ) ) + return $items; - $items = get_posts( $args ); + $defaults = array( 'order' => 'ASC', 'orderby' => 'menu_order', 'post_type' => 'nav_menu_item', 'post_status' => 'publish', 'output' => ARRAY_A, 'output_key' => 'menu_order', 'nopaging' => true, + 'update_post_term_cache' => false); + $args = wp_parse_args( $args, $defaults ); + if ( count( $items ) > 1 ) + $args['include'] = implode( ',', $items ); + else + $args['include'] = $items[0]; - if ( is_wp_error( $items ) || ! is_array( $items ) ) { - return false; - } + $items = get_posts( $args ); - $items = array_map( 'wp_setup_nav_menu_item', $items ); + if ( is_wp_error( $items ) || ! is_array( $items ) ) + return false; - if ( ARRAY_A == $args['output'] ) { - $GLOBALS['_menu_item_sort_prop'] = $args['output_key']; - usort($items, '_sort_nav_menu_items'); - $i = 1; - foreach( $items as $k => $item ) { - $items[$k]->$args['output_key'] = $i++; - } + // Get all posts and terms at once to prime the caches + $posts = array(); + $terms = array(); + foreach ( $items as $item ) { + $object_id = get_post_meta( $item->ID, '_menu_item_object_id', true ); + $object = get_post_meta( $item->ID, '_menu_item_object', true ); + $type = get_post_meta( $item->ID, '_menu_item_type', true ); + + if ( 'post_type' == $type ) + $posts[$object][] = $object_id; + elseif ( 'taxonomy' == $type) + $terms[$object][] = $object_id; + } + + if ( !empty($posts) ) { + foreach ( array_keys($posts) as $post_type ) { + get_posts( array('post__in' => $posts[$post_type], 'post_type' => $post_type, 'nopaging' => true, 'update_post_term_cache' => false) ); } } + unset($posts); + + if ( !empty($terms) ) { + foreach ( array_keys($terms) as $taxonomy ) { + get_terms($taxonomy, array('include' => $terms[$taxonomy]) ); + } + } + unset($terms); + + $items = array_map( 'wp_setup_nav_menu_item', $items ); + + if ( ARRAY_A == $args['output'] ) { + $GLOBALS['_menu_item_sort_prop'] = $args['output_key']; + usort($items, '_sort_nav_menu_items'); + $i = 1; + foreach( $items as $k => $item ) { + $items[$k]->$args['output_key'] = $i++; + } + } + return $items; }