<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[14557] trunk/wp-includes/nav-menu.php:
  Reduce number of queries in wp_get_nav_menu_items()
  by fetching posts and terms with batch queries.</title>
</head>
<body>

<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/14557">14557</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2010-05-11 14:21:03 +0000 (Tue, 11 May 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>Reduce number of queries in wp_get_nav_menu_items() by fetching posts and terms with batch queries. see <a href="http://trac.wordpress.org/ticket/12734">#12734</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesnavmenuphp">trunk/wp-includes/nav-menu.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesnavmenuphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/nav-menu.php (14556 => 14557)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/nav-menu.php        2010-05-11 13:44:40 UTC (rev 14556)
+++ trunk/wp-includes/nav-menu.php        2010-05-11 14:21:03 UTC (rev 14557)
</span><span class="lines">@@ -412,32 +412,61 @@
</span><span class="cx"> 
</span><span class="cx">         $items = get_objects_in_term( $menu-&gt;term_id, 'nav_menu' );
</span><span class="cx"> 
</span><del>-        if ( ! empty( $items ) ) {
-                $defaults = array( 'order' =&gt; 'ASC', 'orderby' =&gt; 'menu_order', 'post_type' =&gt; 'nav_menu_item', 'post_status' =&gt; 'publish', 'output' =&gt; ARRAY_A, 'output_key' =&gt; 'menu_order', 'nopaging' =&gt; true,
-                                                  'update_post_term_cache' =&gt; false);
-                $args = wp_parse_args( $args, $defaults );
-                if ( count( $items ) &gt; 1 )
-                        $args['include'] = implode( ',', $items );
-                else
-                        $args['include'] = $items[0];
</del><ins>+        if ( empty( $items ) )
+                return $items;
</ins><span class="cx"> 
</span><del>-                $items = get_posts( $args );
</del><ins>+        $defaults = array( 'order' =&gt; 'ASC', 'orderby' =&gt; 'menu_order', 'post_type' =&gt; 'nav_menu_item', 'post_status' =&gt; 'publish', 'output' =&gt; ARRAY_A, 'output_key' =&gt; 'menu_order', 'nopaging' =&gt; true,
+                                          'update_post_term_cache' =&gt; false);
+        $args = wp_parse_args( $args, $defaults );
+        if ( count( $items ) &gt; 1 )
+                $args['include'] = implode( ',', $items );
+        else
+                $args['include'] = $items[0];
</ins><span class="cx"> 
</span><del>-                if ( is_wp_error( $items ) || ! is_array( $items ) ) {
-                        return false;
</del><ins>+        $items = get_posts( $args );
+
+        if ( is_wp_error( $items ) || ! is_array( $items ) )
+                return false;
+
+        // 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-&gt;ID, '_menu_item_object_id', true );
+                $object = get_post_meta( $item-&gt;ID, '_menu_item_object', true );
+                $type = get_post_meta( $item-&gt;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' =&gt; $posts[$post_type], 'post_type' =&gt; $post_type, 'nopaging' =&gt; true, 'update_post_term_cache' =&gt; false) );
</ins><span class="cx">                 }
</span><ins>+        }
+        unset($posts);
</ins><span class="cx"> 
</span><del>-                $items = array_map( 'wp_setup_nav_menu_item', $items );
</del><ins>+        if ( !empty($terms) ) {
+                foreach ( array_keys($terms) as $taxonomy ) {
+                        get_terms($taxonomy, array('include' =&gt; $terms[$taxonomy]) );
+                }
+        }
+        unset($terms);
</ins><span class="cx"> 
</span><del>-                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 =&gt; $item ) {
-                                $items[$k]-&gt;$args['output_key'] = $i++;
-                        }
</del><ins>+        $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 =&gt; $item ) {
+                        $items[$k]-&gt;$args['output_key'] = $i++;
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><ins>+
</ins><span class="cx">         return $items;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>