Performance improvements to the REST API

<div style&equals;"text-align&colon;center"><img src&equals;"data&colon;image&sol;gif&semi;base64&comma;R0lGODlhAQABAIAAAAAAAP&sol;&sol;&sol;ywAAAAAAQABAAACAUwAOw&equals;&equals;" fifu-lazy&equals;"1" fifu-data-sizes&equals;"auto" fifu-data-srcset&equals;"https&colon;&sol;&sol;i2&period;wp&period;com&sol;www&period;awordpresscommenter&period;com&sol;wp-content&sol;uploads&sol;2022&sol;10&sol;performance-improvements-to-the-rest-api&period;jpg&quest;ssl&equals;1&w&equals;75&resize&equals;75&ssl&equals;1 75w&comma; https&colon;&sol;&sol;i2&period;wp&period;com&sol;www&period;awordpresscommenter&period;com&sol;wp-content&sol;uploads&sol;2022&sol;10&sol;performance-improvements-to-the-rest-api&period;jpg&quest;ssl&equals;1&w&equals;100&resize&equals;100&ssl&equals;1 100w&comma; https&colon;&sol;&sol;i2&period;wp&period;com&sol;www&period;awordpresscommenter&period;com&sol;wp-content&sol;uploads&sol;2022&sol;10&sol;performance-improvements-to-the-rest-api&period;jpg&quest;ssl&equals;1&w&equals;150&resize&equals;150&ssl&equals;1 150w&comma; https&colon;&sol;&sol;i2&period;wp&period;com&sol;www&period;awordpresscommenter&period;com&sol;wp-content&sol;uploads&sol;2022&sol;10&sol;performance-improvements-to-the-rest-api&period;jpg&quest;ssl&equals;1&w&equals;240&resize&equals;240&ssl&equals;1 240w&comma; https&colon;&sol;&sol;i2&period;wp&period;com&sol;www&period;awordpresscommenter&period;com&sol;wp-content&sol;uploads&sol;2022&sol;10&sol;performance-improvements-to-the-rest-api&period;jpg&quest;ssl&equals;1&w&equals;320&resize&equals;320&ssl&equals;1 320w&comma; https&colon;&sol;&sol;i2&period;wp&period;com&sol;www&period;awordpresscommenter&period;com&sol;wp-content&sol;uploads&sol;2022&sol;10&sol;performance-improvements-to-the-rest-api&period;jpg&quest;ssl&equals;1&w&equals;500&resize&equals;500&ssl&equals;1 500w&comma; https&colon;&sol;&sol;i2&period;wp&period;com&sol;www&period;awordpresscommenter&period;com&sol;wp-content&sol;uploads&sol;2022&sol;10&sol;performance-improvements-to-the-rest-api&period;jpg&quest;ssl&equals;1&w&equals;640&resize&equals;640&ssl&equals;1 640w&comma; https&colon;&sol;&sol;i2&period;wp&period;com&sol;www&period;awordpresscommenter&period;com&sol;wp-content&sol;uploads&sol;2022&sol;10&sol;performance-improvements-to-the-rest-api&period;jpg&quest;ssl&equals;1&w&equals;800&resize&equals;800&ssl&equals;1 800w&comma; https&colon;&sol;&sol;i2&period;wp&period;com&sol;www&period;awordpresscommenter&period;com&sol;wp-content&sol;uploads&sol;2022&sol;10&sol;performance-improvements-to-the-rest-api&period;jpg&quest;ssl&equals;1&w&equals;1024&resize&equals;1024&ssl&equals;1 1024w&comma; https&colon;&sol;&sol;i2&period;wp&period;com&sol;www&period;awordpresscommenter&period;com&sol;wp-content&sol;uploads&sol;2022&sol;10&sol;performance-improvements-to-the-rest-api&period;jpg&quest;ssl&equals;1&w&equals;1280&resize&equals;1280&ssl&equals;1 1280w&comma; https&colon;&sol;&sol;i2&period;wp&period;com&sol;www&period;awordpresscommenter&period;com&sol;wp-content&sol;uploads&sol;2022&sol;10&sol;performance-improvements-to-the-rest-api&period;jpg&quest;ssl&equals;1&w&equals;1600&resize&equals;1600&ssl&equals;1 1600w" width&equals;"620" height&equals;"413" fifu-data-src&equals;"https&colon;&sol;&sol;i2&period;wp&period;com&sol;www&period;awordpresscommenter&period;com&sol;wp-content&sol;uploads&sol;2022&sol;10&sol;performance-improvements-to-the-rest-api&period;jpg&quest;ssl&equals;1" class&equals;"attachment-post-thumbnail size-post-thumbnail wp-post-image" alt&equals;"Performance improvements to the REST API" title&equals;"Performance improvements to the REST API" srcset&equals;"https&colon;&sol;&sol;i2&period;wp&period;com&sol;www&period;awordpresscommenter&period;com&sol;wp-content&sol;uploads&sol;2022&sol;10&sol;performance-improvements-to-the-rest-api&period;jpg&quest;ssl&equals;1 620w&comma; https&colon;&sol;&sol;www&period;awordpresscommenter&period;com&sol;wp-content&sol;uploads&sol;2022&sol;10&sol;performance-improvements-to-the-rest-api-300x200&period;jpg 300w" sizes&equals;"&lpar;max-width&colon; 620px&rpar; 100vw&comma; 620px" &sol;><&sol;div><div>&NewLine;<p>WordPress 6&period;1 brings a number of key improvements to the REST API to increase performance&period; These improvements decrease the number of database queries that are run on each REST API request&period; <&sol;p>&NewLine;<h2>Avoid unnecessarily preparing item links<&sol;h2>&NewLine;<p>Prior to WordPress 6&period;1&comma; the <code>prepare&lowbar;links<&sol;code> method in the REST API was called in all controllers&period; If the <code>&lowbar;fields<&sol;code> parameter is passed to the REST API request&comma; it might mean that the links field is not requested and would never be returned in the response&period; This is wasteful&comma; as <code>prepare&lowbar;links<&sol;code> can contain database calls or other complex logic that would be run even if it never returned the response&period; <&sol;p>&NewLine;<p>In 6&period;1 <code>prepare&lowbar;links<&sol;code> are only called if requested in the response&comma; when links are requested in fields or the <code>&lowbar;embedded<&sol;code> parameter is passed&period; As part of this work&comma; the taxonomy and post type controllers have now been updated to implement said <code>prepare&lowbar;links<&sol;code> method to bring them in line with other REST API controllers&period; <&sol;p>&NewLine;<p>This is the code example of implementing this change in custom REST API controllers&period;<&sol;p>&NewLine;<p><strong>Before<&sol;strong><&sol;p>&NewLine;<div class&equals;"wp-block-syntaxhighlighter-code ">&NewLine;<pre class&equals;"brush&colon; php&semi; title&colon; &semi; notranslate">&NewLine;&dollar;response &equals; rest&lowbar;ensure&lowbar;response&lpar; &dollar;data &rpar;&semi;&NewLine;&dollar;links &equals; &dollar;this->prepare&lowbar;links&lpar; &dollar;post &rpar;&semi;&NewLine;&dollar;response->add&lowbar;links&lpar; &dollar;links &rpar;&semi;&NewLine;&NewLine;return &dollar;response&semi;&NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p><strong>After<&sol;strong><&sol;p>&NewLine;<div class&equals;"wp-block-syntaxhighlighter-code ">&NewLine;<pre class&equals;"brush&colon; php&semi; title&colon; &semi; notranslate">&NewLine;&dollar;response &equals; rest&lowbar;ensure&lowbar;response&lpar; &dollar;data &rpar;&semi;&NewLine;&NewLine;if &lpar; rest&lowbar;is&lowbar;field&lowbar;included&lpar; '&lowbar;links'&comma; &dollar;fields &rpar; &vert;&vert; rest&lowbar;is&lowbar;field&lowbar;included&lpar; '&lowbar;embedded'&comma; &dollar;fields &rpar; &rpar; &lbrace;&NewLine;  &dollar;links &equals; &dollar;this->prepare&lowbar;links&lpar; &dollar;post &rpar;&semi;&NewLine; &dollar;response->add&lowbar;links&lpar; &dollar;links &rpar;&semi;&NewLine;&rcub;&NewLine;&NewLine;return &dollar;response&semi;&NewLine;<&sol;pre>&NewLine;<&sol;div>&NewLine;<p>This logic conditionally calls prepare&lowbar;links <em>only<&sol;em> if <code>&lowbar;links<&sol;code> or <code>&lowbar;embedded<&sol;code> is requested&period; <&sol;p>&NewLine;<p>For more info see Trac ticket&colon; <a href&equals;"https&colon;&sol;&sol;core&period;trac&period;wordpress&period;org&sol;ticket&sol;52992">&num;52992<&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;core&period;trac&period;wordpress&period;org&sol;ticket&sol;56019">&num;56019<&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;core&period;trac&period;wordpress&period;org&sol;ticket&sol;56020">&num;56020<&sol;a><&sol;p>&NewLine;<h2>Improvement to the Posts controller<&sol;h2>&NewLine;<p>When running profiling tools against the responses of REST API requests&comma; it was discovered that post controllers request a lot of linked data to each post&period; For example&comma; when returning a post in a REST API response&comma; linked data such as author &lpar;user&rpar;&comma; featured image&comma; and parent post were all requested&period; As these linked items were not primed in caches&comma; it could mean that for each post in the REST API response there would be 3 separate database queries&colon; one for the user&comma; one for the featured image&comma; and another for the parent post&period; <&sol;p>&NewLine;<p>In WordPress 6&period;1 all the caches are primed in a single database query and there are new helper functions to enable this&colon;<&sol;p>&NewLine;<p><code>update&lowbar;post&lowbar;author&lowbar;caches<&sol;code><&sol;p>&NewLine;<p>Takes an array of posts and primes users caches in a single query&period; <&sol;p>&NewLine;<p><code>update&lowbar;post&lowbar;parent&lowbar;caches<&sol;code><&sol;p>&NewLine;<p>Takes an array of posts and primes post parents in a single query&period; <&sol;p>&NewLine;<p><code>update&lowbar;menu&lowbar;item&lowbar;cache<&sol;code><&sol;p>&NewLine;<p>Takes an array of posts and primes post &sol; terms link to menu items single query&period; <&sol;p>&NewLine;<p>The existing function <code>update&lowbar;post&lowbar;thumbnail&lowbar;cache<&sol;code> was used to prime featured image caches&period; These functions are also being rolled out to other parts of the core that can benefit from priming caches in a single place&period; <&sol;p>&NewLine;<p>For more info see Trac tickets&colon; <a href&equals;"https&colon;&sol;&sol;core&period;trac&period;wordpress&period;org&sol;ticket&sol;55592">&num;55592<&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;core&period;trac&period;wordpress&period;org&sol;ticket&sol;55593">&num;55593<&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;core&period;trac&period;wordpress&period;org&sol;ticket&sol;55620">&num;55620<&sol;a>    <&sol;p>&NewLine;<h2>Improvements to other controllers<&sol;h2>&NewLine;<p>The comments and user controllers have also been improved&colon; User controller now primes user meta in a single query and the comments controller now primes the linked post cache in a single query&period; Improvements were made to the post search controller to improve database performance along with the media controller&period; <&sol;p>&NewLine;<p>For more info see Trac tickets&colon; <a href&equals;"https&colon;&sol;&sol;core&period;trac&period;wordpress&period;org&sol;ticket&sol;55674">&num;55674<&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;core&period;trac&period;wordpress&period;org&sol;ticket&sol;56272">&num;56272<&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;core&period;trac&period;wordpress&period;org&sol;ticket&sol;55677">&num;55677<&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;core&period;trac&period;wordpress&period;org&sol;ticket&sol;55716">&num;55716<&sol;a><&sol;p>&NewLine;<p><em>Thanks to <&sol;em><a href&equals;"https&colon;&sol;&sol;profiles&period;wordpress&period;org&sol;flixos90&sol;" class&equals;"mention"><span class&equals;"mentions-prefix">&commat;<&sol;span>flixos90<&sol;a><em> and <&sol;em><a href&equals;"https&colon;&sol;&sol;profiles&period;wordpress&period;org&sol;milana&lowbar;cap&sol;" class&equals;"mention"><span class&equals;"mentions-prefix">&commat;<&sol;span>milana&lowbar;cap<&sol;a> <em>for peer review<&sol;em> and <a href&equals;"https&colon;&sol;&sol;profiles&period;wordpress&period;org&sol;shetheliving&sol;" class&equals;"mention"><span class&equals;"mentions-prefix">&commat;<&sol;span>shetheliving<&sol;a> for <em>proofreading<&sol;em>&period; <&sol;p>&NewLine;<p class&equals;"o2-appended-tags"><a href&equals;"https&colon;&sol;&sol;make&period;wordpress&period;org&sol;core&sol;tag&sol;6-1&sol;" class&equals;"tag"><span class&equals;"tag-prefix">&num;<&sol;span>6-1<&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;make&period;wordpress&period;org&sol;core&sol;tag&sol;core-restapi&sol;" class&equals;"tag"><span class&equals;"tag-prefix">&num;<&sol;span>core-restapi<&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;make&period;wordpress&period;org&sol;core&sol;tag&sol;dev-notes&sol;" class&equals;"tag"><span class&equals;"tag-prefix">&num;<&sol;span>dev-notes<&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;make&period;wordpress&period;org&sol;core&sol;tag&sol;dev-notes-6-1&sol;" class&equals;"tag"><span class&equals;"tag-prefix">&num;<&sol;span>dev-notes-6-1<&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;make&period;wordpress&period;org&sol;core&sol;tag&sol;performance&sol;" class&equals;"tag"><span class&equals;"tag-prefix">&num;<&sol;span>performance<&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;make&period;wordpress&period;org&sol;core&sol;tag&sol;rest-api&sol;" class&equals;"tag"><span class&equals;"tag-prefix">&num;<&sol;span>rest-api<&sol;a><&sol;p>&NewLine;<&sol;div>&NewLine;

Sponsored
Sponsored
A WordPress Commenter

Recent Posts

Judge Reins In Discovery Fight as WP Engine Ordered to Produce Customer Data

A U.S. magistrate judge has moved to rein in the escalating discovery battle between WP…

7 hours ago

Ollie’s Menu Designer Flagged for Core, With Automattic Developers Set to Help Shepherd It

Ollie’s “game-changing” Menu Designer plugin is moving toward inclusion in WordPress core. Anne McCarthy says…

4 days ago

WordPress.com Rolls Out MCP Support as Automattic Dogfoods MCP Adapter

WordPress.com has rolled out support for the Model Context Protocol (MCP), allowing AI assistants to…

5 days ago

First Ten Hosts Certified Under Secure Hosting Alliance’s Trust Seal Program

Ten hosting providers have been certified under the Secure Hosting Alliance’s new Trust Seal program,…

5 days ago

Chasing the Sun: Inside WP Accessibility Day’s 24-Hour Event & What to Expect in 2025

When WP Accessibility Day kicks off next week, someone, somewhere will always be awake. As…

5 days ago

Performance Chat Summary: 7 October 2025

The full chat log is available beginning here on Slack. WordPress Performance Trac tickets @westonruter…

6 days ago