Join Morten Rand-Hendriksen for an in-depth discussion in this video Embed additional data in the REST API response for a post, part of WordPress: REST API.
- [Instructor] In the previous movie I outlined a situation where the default response from the wordpress rest API for a post isn't enough. That situation is as follows, I want to display the contents of the post in an app. And within the content I want to have the post title, the post content of course, and then I also want the author name and the featured image. The problem is if we just request a post object itself, we don't get all the information. We have the post title, and the post content, and all the meta information about the post, but when it comes to the author name, the featured media item, the categories or tags, or comments or anything else, we don't have the information itself in the post object, all we have are links, and references to those items.
So here we have the author with the id 10, and a featured image with the id of 3542. To get that information I have to make another request to the rest API to say hey can you give me author number 10? And I can do that if I go down to the links, here we have the link to the author, I can follow that link in a separate tab, run it and here we get all the information for that author. It's me, Morten Rand-Hendrickson, here's my URL, and my description and so on. So I could do that. I could request a post and then say okay, now you have the post, find id for the author, or the link, go to the next link, grab that information and collate it into the post.
But that would be really clunky, especially if we also wanted all the categories and tags and maybe comments or something else. The good news is we don't have to do that. We can get the API to collate all this data into the single post object. Well anytime you see embeddable, true, on one of these links, it means the API will allow us to embed the return of that item into the post object itself. So in effect, we would request a post, and then in addition to the post, we'll also make a request for the author, and for the replies, so those are the comments, and for the featured media, and for all the categories, and all the tags.
How is that done? Really simple. Go up in the URL, type in question mark, underscore, embed equals true, send that, and now we get a much bigger post object back. We start off the same way as before, here we have the ID and the content and excerpt, including all the links, then we scroll past the links, and here we have an embedded section, starting with the author, ID, 10. Name Morten Rand-Hendrickson, and so on and so on. And you'll notice, this is the exact same post object I just requested in a separate tab, then scroll further down, we have the featured media, here we have the title for the featured media object, and we have the links to all the different image sizes, and any other information that's associated with it.
Scrolling further down still, we have the terms, and here we have each of the categories, and each of the tags, and if there were comments for the post we'd also get each of the comment object inside of this return. So by adding this underscore embed equals true to the post request, we not only get the post, but we get all associated data as well. That makes our job as developers much easier, we don't have to keep going back and forth through the browser to request more data, we can get it all mushed together in one single request.
Of course it does mean we increase the size of the returned data but we're just returning raw data here, there's no styles or java script or anything else, so even increasing the size makes almost no difference in terms of performance. So if you work with single posts, and you want this additional information, it's probably a good idea to set embed to true, so you get all the information rather than going back and forth. Of course it's up to you and you can do it either way, but in this course we'll be using this embed feature to get the information we need to display the content we want on the front end.
- Using a REST client to communicate with a REST API
- Reviewing available routes, endpoints, and arguments
- Adding custom post types and taxonomies
- Altering the API response
- Using PHP for resiliency
- Passing info using wp_localize_script()
- Setting up a REST API request through AJAX
- Outputting selected post data to the front end
- Formatting post data to match a theme
- Triggering AJAX for an infinite scroll effect
- Using AJAX to load subsequent responses