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 _embed to a WordPress REST API request to get linked data in one call

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

curl -s "https://example.com/wp-json/wp/v2/posts/42?_embed"

One extra query parameter pulls authors and other linked resources into the same response.

A post from the WordPress REST API gives you an author ID, and then you need a second request to learn who that is. That gets old quickly. There’s a query parameter for it, and I wish more tutorials started with it.

curl -s "https://example.com/wp-json/wp/v2/posts/42?_embed"

Embedding is triggered by the _embed query parameter. Every embeddable link in the response is then resolved for you. The full resource shows up in an _embedded object, right next to _links. In the official example the post’s author link is marked "embeddable": true, so the author’s data (id, name, description) arrives in the same response.

The _embedded object mirrors the structure of _links. If you only want to see which relations were embedded, pipe the response through jq:

curl -s "https://example.com/wp-json/wp/v2/posts/42?_embed" | jq '._embedded | keys'

It works on collections too, for example /wp/v2/posts?author=1&_embed. That is where it saves the most, because you skip one request per post.

Two caveats from the docs. First, when you embed a collection, the embedded collection uses the default pagination limits, so don’t expect a full list of everything. Second, only relations that have an embedded response appear in _embedded, and if a relation mixes embeddable and non-embeddable links, placeholder responses fill the gaps so the indexes still line up with _links. The response also gets bigger, so for a mobile client or a slow connection I’d think twice before embedding everything.

Further reading: Linking and embedding

More WordPress tips curl -s -D – -o /dev/null "https://example.com/wp-json/wp/v2/posts?per_page=100" curl -s "https://example.com/?rest_route=/" 'You must be logged in.', add_filter( 'the_title', 'plugin_shout_title', 20 ); define( 'AUTOMATIC_UPDATER_DISABLED', true ); All WordPress tips (92) All topics

Link