brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: add _fields to a WordPress REST API request to get a smaller answer

WordPress
~/tips/tip-wordpress-rest-api-fields

curl "https://example.com/wp-json/wp/v2/posts?_fields=id,title,link"

Makes the WordPress REST API return only the fields you list, so responses are smaller and faster.

A single post from /wp-json/wp/v2/posts comes back as a big pile of JSON. Most of it I never read.

The REST API has a global parameter called _fields. You list the properties you want, comma separated, and WordPress returns only those. The docs point out that skipping the rest avoids extra work on the server and gives you a smaller response that downloads and parses faster.

curl "https://example.com/wp-json/wp/v2/posts?_fields=id,title,link"

Replace the domain with your own. The answer is a list of posts with just an ID, a title and a link. Nested properties work too, from WordPress 5.3 on. For a single meta key you write something like _fields=meta.my_key.

The caveat shows up when you combine it with _embed. Embedded items arrive under _embedded and the links under _links, so if you filter fields you have to name those two as well, or the embedded data vanishes.

Use it in any script, headless frontend or widget that reads from the API. It costs one query parameter.

That is a decent habit for any code that only needs a list of titles. Nothing else about the request changes, so it is easy to add to an existing script.

Further reading: REST API Handbook: Global Parameters, On this site: wp option list –autoload=on –format=total_bytes shows what every page load drags along, On this site: wp transient delete –expired cleans the leftovers out of your options table

More WordPress tips wp user application-password create anna backup-script --porcelain define( 'MEDIA_TRASH', true ); wp cli check-update wp theme delete --all wp media image-size All WordPress tips (85) All topics

Link