<div style="text-align:center"><img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==" fifu-lazy="1" fifu-data-sizes="auto" fifu-data-srcset="https://i2.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/performance-improvements-to-the-rest-api.jpg?ssl=1&w=75&resize=75&ssl=1 75w, https://i2.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/performance-improvements-to-the-rest-api.jpg?ssl=1&w=100&resize=100&ssl=1 100w, https://i2.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/performance-improvements-to-the-rest-api.jpg?ssl=1&w=150&resize=150&ssl=1 150w, https://i2.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/performance-improvements-to-the-rest-api.jpg?ssl=1&w=240&resize=240&ssl=1 240w, https://i2.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/performance-improvements-to-the-rest-api.jpg?ssl=1&w=320&resize=320&ssl=1 320w, https://i2.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/performance-improvements-to-the-rest-api.jpg?ssl=1&w=500&resize=500&ssl=1 500w, https://i2.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/performance-improvements-to-the-rest-api.jpg?ssl=1&w=640&resize=640&ssl=1 640w, https://i2.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/performance-improvements-to-the-rest-api.jpg?ssl=1&w=800&resize=800&ssl=1 800w, https://i2.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/performance-improvements-to-the-rest-api.jpg?ssl=1&w=1024&resize=1024&ssl=1 1024w, https://i2.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/performance-improvements-to-the-rest-api.jpg?ssl=1&w=1280&resize=1280&ssl=1 1280w, https://i2.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/performance-improvements-to-the-rest-api.jpg?ssl=1&w=1600&resize=1600&ssl=1 1600w" width="620" height="413" fifu-data-src="https://i2.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/performance-improvements-to-the-rest-api.jpg?ssl=1" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="Performance improvements to the REST API" title="Performance improvements to the REST API" srcset="https://i2.wp.com/www.awordpresscommenter.com/wp-content/uploads/2022/10/performance-improvements-to-the-rest-api.jpg?ssl=1 620w, https://www.awordpresscommenter.com/wp-content/uploads/2022/10/performance-improvements-to-the-rest-api-300x200.jpg 300w" sizes="(max-width: 620px) 100vw, 620px" /></div><div>
<p>WordPress 6.1 brings a number of key improvements to the REST API to increase performance. These improvements decrease the number of database queries that are run on each REST API request. </p>
<h2>Avoid unnecessarily preparing item links</h2>
<p>Prior to WordPress 6.1, the <code>prepare_links</code> method in the REST API was called in all controllers. If the <code>_fields</code> parameter is passed to the REST API request, it might mean that the links field is not requested and would never be returned in the response. This is wasteful, as <code>prepare_links</code> can contain database calls or other complex logic that would be run even if it never returned the response. </p>
<p>In 6.1 <code>prepare_links</code> are only called if requested in the response, when links are requested in fields or the <code>_embedded</code> parameter is passed. As part of this work, the taxonomy and post type controllers have now been updated to implement said <code>prepare_links</code> method to bring them in line with other REST API controllers. </p>
<p>This is the code example of implementing this change in custom REST API controllers.</p>
<p><strong>Before</strong></p>
<div class="wp-block-syntaxhighlighter-code ">
<pre class="brush: php; title: ; notranslate">
$response = rest_ensure_response( $data );
$links = $this->prepare_links( $post );
$response->add_links( $links );

return $response;
</pre>
</div>
<p><strong>After</strong></p>
<div class="wp-block-syntaxhighlighter-code ">
<pre class="brush: php; title: ; notranslate">
$response = rest_ensure_response( $data );

if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
 $links = $this->prepare_links( $post );
 $response->add_links( $links );
}

return $response;
</pre>
</div>
<p>This logic conditionally calls prepare_links <em>only</em> if <code>_links</code> or <code>_embedded</code> is requested. </p>
<p>For more info see Trac ticket: <a href="https://core.trac.wordpress.org/ticket/52992">#52992</a>, <a href="https://core.trac.wordpress.org/ticket/56019">#56019</a>, <a href="https://core.trac.wordpress.org/ticket/56020">#56020</a></p>
<h2>Improvement to the Posts controller</h2>
<p>When running profiling tools against the responses of REST API requests, it was discovered that post controllers request a lot of linked data to each post. For example, when returning a post in a REST API response, linked data such as author (user), featured image, and parent post were all requested. As these linked items were not primed in caches, it could mean that for each post in the REST API response there would be 3 separate database queries: one for the user, one for the featured image, and another for the parent post. </p>
<p>In WordPress 6.1 all the caches are primed in a single database query and there are new helper functions to enable this:</p>
<p><code>update_post_author_caches</code></p>
<p>Takes an array of posts and primes users caches in a single query. </p>
<p><code>update_post_parent_caches</code></p>
<p>Takes an array of posts and primes post parents in a single query. </p>
<p><code>update_menu_item_cache</code></p>
<p>Takes an array of posts and primes post / terms link to menu items single query. </p>
<p>The existing function <code>update_post_thumbnail_cache</code> was used to prime featured image caches. These functions are also being rolled out to other parts of the core that can benefit from priming caches in a single place. </p>
<p>For more info see Trac tickets: <a href="https://core.trac.wordpress.org/ticket/55592">#55592</a>, <a href="https://core.trac.wordpress.org/ticket/55593">#55593</a>, <a href="https://core.trac.wordpress.org/ticket/55620">#55620</a> </p>
<h2>Improvements to other controllers</h2>
<p>The comments and user controllers have also been improved: User controller now primes user meta in a single query and the comments controller now primes the linked post cache in a single query. Improvements were made to the post search controller to improve database performance along with the media controller. </p>
<p>For more info see Trac tickets: <a href="https://core.trac.wordpress.org/ticket/55674">#55674</a>, <a href="https://core.trac.wordpress.org/ticket/56272">#56272</a>, <a href="https://core.trac.wordpress.org/ticket/55677">#55677</a>, <a href="https://core.trac.wordpress.org/ticket/55716">#55716</a></p>
<p><em>Thanks to </em><a href="https://profiles.wordpress.org/flixos90/" class="mention"><span class="mentions-prefix">@</span>flixos90</a><em> and </em><a href="https://profiles.wordpress.org/milana_cap/" class="mention"><span class="mentions-prefix">@</span>milana_cap</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/core-restapi/" class="tag"><span class="tag-prefix">#</span>core-restapi</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>, <a href="https://make.wordpress.org/core/tag/rest-api/" class="tag"><span class="tag-prefix">#</span>rest-api</a></p>
</div>

A U.S. magistrate judge has moved to rein in the escalating discovery battle between WP…
Ollie’s “game-changing” Menu Designer plugin is moving toward inclusion in WordPress core. Anne McCarthy says…
WordPress.com has rolled out support for the Model Context Protocol (MCP), allowing AI assistants to…
Ten hosting providers have been certified under the Secure Hosting Alliance’s new Trust Seal program,…
When WP Accessibility Day kicks off next week, someone, somewhere will always be awake. As…
The full chat log is available beginning here on Slack. WordPress Performance Trac tickets @westonruter…