<div style="text-align:center"><img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==" fifu-lazy="1" fifu-data-sizes="auto" fifu-data-srcset="https://i1.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1.jpg?ssl=1&w=75&resize=75&ssl=1 75w, https://i1.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1.jpg?ssl=1&w=100&resize=100&ssl=1 100w, https://i1.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1.jpg?ssl=1&w=150&resize=150&ssl=1 150w, https://i1.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1.jpg?ssl=1&w=240&resize=240&ssl=1 240w, https://i1.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1.jpg?ssl=1&w=320&resize=320&ssl=1 320w, https://i1.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1.jpg?ssl=1&w=500&resize=500&ssl=1 500w, https://i1.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1.jpg?ssl=1&w=640&resize=640&ssl=1 640w, https://i1.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1.jpg?ssl=1&w=800&resize=800&ssl=1 800w, https://i1.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1.jpg?ssl=1&w=1024&resize=1024&ssl=1 1024w, https://i1.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1.jpg?ssl=1&w=1280&resize=1280&ssl=1 1280w, https://i1.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1.jpg?ssl=1&w=1600&resize=1600&ssl=1 1600w" width="1520" height="800" fifu-data-src="https://i1.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1.jpg?ssl=1" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="Improvements to WP_Query performance in 6.1" title="Improvements to WP_Query performance in 6.1" srcset="https://i1.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1.jpg?ssl=1 1520w, https://www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1-300x158.jpg 300w, https://www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1-1024x539.jpg 1024w, https://www.awordpresscommenter.com/wp-content/uploads/2022/10/improvements-to-wp_query-performance-in-6-1-768x404.jpg 768w" sizes="(max-width: 1520px) 100vw, 1520px" /></div><div>
<h2>Adding caching to database queries in <code>WP_Query</code></h2>
<p>WordPress 6.1 includes an improvement to how database queries are performed in the <code>WP_Query</code> class, resulting in database queries will be cached. This means that if the same database query is run more than once, the result will be loaded from cache. For those using persistent object caching, this will mean that the database query will not run again until caches are invalidated, resulting in far fewer queries to the database. Sites using in-memory caching will also see the benefit of not repeating these queries, though the performance improvement will not be as significant. </p>
<p>For those doing custom development, please ensure that you are using core functions such as <code>wp_insert_post</code> to add posts to the database. These functions are well-maintained, and by using them, you ensure that caches are correctly invalidated. If you are updating the database directly, then it is strongly recommended that you call the <code>clean_post_cache</code> function after updating the database row. </p>
<p>It is worth noting that by default, all calls to <code>WP_Query</code> will be cached going forward. It is possible to opt out of caching queries by simply passing the <code>cache_results</code> parameter as <code>false</code>. See example:</p>
<div class="wp-block-syntaxhighlighter-code ">
<pre class="brush: php; title: ; notranslate">
$args = array(
 'posts_per_page' => 50,
 'cache_results' => false
);
$query = new WP_Query( $args );
</pre>
</div>
<p>It is also possible to globally disable caching, using a filter:</p>
<div class="wp-block-syntaxhighlighter-code ">
<pre class="brush: php; title: ; notranslate">
function disable_caching( $wp_query ) {
 $wp_query->query_vars['cache_results'] = false;
}
add_action( 'parse_query', 'disable_caching' );
</pre>
</div>
<p><strong>Disabling caching like this should only be done in extreme cases. </strong>For best performance, it is highly recommended to keep caching enabled and invalidate caches using the <code>clean_post_cache</code> function. </p>
<p>Cache keys are generated using the parameters passed to the <code>WP_Query</code> class instance. However, the following parameters are ignored:</p>
<ul>
<li><code>suppress_filters</code></li>
<li><code>cache_results</code></li>
<li><code>fields</code></li>
<li><code>update_post_meta_cache</code></li>
<li><code>update_post_term_cache</code></li>
<li><code>update_menu_item_cache</code></li>
<li><code>lazy_load_term_meta</code></li>
</ul>
<p>These parameters do not affect the database query that is run. The most important ignored parameter is <code>fields</code>. This means that if you run the following: </p>
<div class="wp-block-syntaxhighlighter-code ">
<pre class="brush: php; title: ; notranslate">
$args1 = array(
	'posts_per_page' => 50,
	'fields' => 'ids'
);
$query1 = new WP_Query( $args1 );

$args2 = array(
	'posts_per_page' => 50,
	'fields' => 'all'
);
$query2 = new WP_Query( $args2 );

</pre>
</div>
<p>In both cases, the query will now request all fields, so that the result can be cached and then be used regardless of the <code>fields</code> parameter. Prior to this change, the database query for those two was different, but keeping it like that would have resulted in multiple caches for effectively subsets of the same data. This means that there is now less of a performance improvement when limiting <code>fields</code> ids than there was in the previous version of WordPress. </p>
<p>This change also means that the <code>update_post_meta_cache</code> and <code>update_post_term_cache</code> caches are always respected. </p>
<p>In cases where caching was added to <code>WP_Query</code> by using plugins such as <a href="https://github.com/Automattic/advanced-post-cache/">advanced-post-cache</a>, <a href="https://github.com/TimeIncUK/enhanced-post-cache">Enhanced Post Cache</a> or <a href="https://github.com/sc0ttkclark/cache-wp-query">Cache WP_Query</a>, it is recommended that these plugins are disabled and removed as they are no longer required.</p>
<p>For more info, see Trac ticket <a href="https://core.trac.wordpress.org/ticket/22176">#22176</a></p>
<h2>Prime users cache in <code>WP_Query</code></h2>
<p>WordPress 6.1 introduces a new function, <code>update_post_author_caches</code>. Prior to 6.1, sites with multiple authors required several single database queries to get author information, as the user is loaded as part of the loop. Instead of loading each user one by one, user (author) caches are now primed in a single database call by calling <code>update_post_author_caches</code> at the start of the loop, resulting in far fewer database queries. </p>
<p>This function accepts an array of post objects and will prime user caches. Calls to <code>update_post_author_caches</code> have also been added in key parts of the codebase to improve database performance.</p>
<p>For more info, see Trac ticket <a href="https://core.trac.wordpress.org/ticket/55716">#55716</a></p>
<h2>Prime linked objects for menu items</h2>
<p>A new function has been added to core called <code>update_menu_item_cache</code>. It accepts an array of post objects and will prime caches for post or terms referenced in a menu item. A new parameter for <code>WP_Query</code> has been added called <code>update_menu_item_cache</code>. When set to <code>true</code> it will call <code>update_menu_item_cache</code> which will allow you to prime menu items in a two database queries (one for posts and one for terms). </p>
<p>For more info, see Trac ticket <a href="https://core.trac.wordpress.org/ticket/55620">#55620</a></p>
<h2><code>get_page_by_title</code> now uses <code>WP_Query</code></h2>
<p>The function <code>get_page_by_title</code> now uses <code>WP_Query</code>. Previously, this function used a raw database query to get the page by title. As noted above, <code>WP_Query</code> is now cached, meaning that calls to <code>get_page_by_title</code> will also be cached. This also has the benefit of running through all the filters in <code>WP_Query</code>. </p>
<p>For more info, see Trac ticket <a href="https://core.trac.wordpress.org/ticket/36905">#36905</a> </p>
<p class="has-text-align-right"><em>Thanks to </em><a href="https://profiles.wordpress.org/flixos90/" class="mention"><span class="mentions-prefix">@</span>flixos90</a><em>, </em><a href="https://profiles.wordpress.org/milana_cap/" class="mention"><span class="mentions-prefix">@</span>milana_cap</a><em>, </em><a href="https://profiles.wordpress.org/peterwilsoncc/" class="mention"><span class="mentions-prefix">@</span>peterwilsoncc</a> <em>for peer review</em> and <a href="https://profiles.wordpress.org/shetheliving/" class="mention"><span class="mentions-prefix">@</span>shetheliving</a> for <em>proofreading</em>. </p>
<p class="o2-appended-tags"><a href="https://make.wordpress.org/core/tag/6-1/" class="tag"><span class="tag-prefix">#</span>6-1</a>, <a href="https://make.wordpress.org/core/tag/dev-notes/" class="tag"><span class="tag-prefix">#</span>dev-notes</a>, <a href="https://make.wordpress.org/core/tag/dev-notes-6-1/" class="tag"><span class="tag-prefix">#</span>dev-notes-6-1</a>, <a href="https://make.wordpress.org/core/tag/performance/" class="tag"><span class="tag-prefix">#</span>performance</a></p>
</div>

Automattic has again asked the U.S. District Court to throw out WP Engine’s antitrust and…
A new ecommerce contender has entered the WordPress space. This week, WPManageNinja officially launched FluentCart,…
The WP Minute is stepping into education. The publication best known for its interviews, podcasts,…
AI-powered site builders are having a moment in WordPress, and 10Web is the latest company…
For as long as Aaron Edwards and Joshua Dailey have known each other, they’ve been…
A U.S. magistrate judge has moved to rein in the escalating discovery battle between WP…