Join Morten Rand-Hendriksen for an in-depth discussion in this video Detailed look at the post REST API response, part of WordPress: REST API.
- [Instructor] Now that you're familiar with the structure of the WordPress Rest API and you know how to request content from the API using routes, endpoints, and arguments, it's time to take a look at what you actually get when you request an object. In this movie and the next one, we'll inspect the single post object and see what is returned from the API and how we can work with it. To start off, we'll just request a single post, and I'll do that by first setting my environment to restful.dev, then going to my Rest API collections and selecting Posts.
This gives me a list of the 10 most recent posts. Here we have the first item, and this is actually the full post object, but I want only that single object to work with. I'll scroll down to the links section, and, here, the first link is to route to the post item itself directly from the API. If I click that link, a separate tab opens, it points at the link in question, and I can click Send to get just that one post object. Now we can look at what is returned to us from the API. This post object starts with a ID for the post, 3541.
That's the database ID we'll use to reference it. Then we have the published date for the post. This is the local date as set inside WordPress and the GMT version of that date. We have the rendered guid link. This is the link you would use on the front end in a regular web browser, and this is the raw link that's being sent to the database. We have the modified date, again in local time and GMT. Then we have the post slug that's auto-generated by WordPress when you create a new post, and then the link to the post.
This is the human readable link or whatever permalink is set inside WordPress. After all this metacontent, we get to the content of the post itself. We start with the title, rendered out as HTML, The Four Hours. Then we have the content, again rendered out as HTML, and this is the content that is inserted into the WYSIWYG editor and the WordPress editor. For the content, we have a protected property that's set to false. If you password-protect a post, that value will be set to true instead. Under content we have excerpt, again rendered out as HTML, and, again, with a protected property set to false.
Then we have more metadata. Here we have the author ID, the featured media ID, that would be the featured image for the current post, comment status set to open, ping status set to closed. This is not a sticky post, so sticky is set to false. The template that's being used is just a standard single template. If there was a custom template in use, that would be listed here. The post format for the post, which is currently standard. Meta contains all of the custom fields that are applied to a post. I don't have any here, but, if you use either the custom fields in WordPress or a custom fields plugin, those would all be listed here.
Then we have a list of IDs for the categories and tags applied to the current post, and, finally, we have a long list of links. These links are probably the most important component of the output if you ignore the actual post itself. The links section provides us with direct routes to pertinent information about either the post itself or other associated items. Off the top, we have the post itself, the URL directly to the route for the post. Then we have a link to the collection.
This is a collection the post lives inside. In this case, it's a post. That means the collection is posts. If we had a custom post type, this collection would be different. We have the route to the information about the current post type in use, in this case types/post. Then we have the routes to the author objects. This would be all the information about the author, including name and website and bio and so on, a link to the replies. These are all the comments for the post, a link to version history, one to wp:featuredmedia, so this would be the featured image, one to wp:attachment.
These are all the images and other media elements that are attached to the current post, and, to round things out, we have wp:term which lists out all the different taxonomies applied to the current post, and, because this is a standard WordPress install, posts have two taxonomies, categories and tags, and, if you had additional custom taxonomies on your site, these would also be listed here. This last one, curies, points back to the w.org API, which is just WordPress API telling whatever is reading this content this is, in fact, content from the WordPress Rest API.
What I'm sure you've noticed is this content is pretty much just a dump of the database entry for the current post. Every single piece of information that's listed in this post object is what you would find if you just went to the database and read that table entry, and that's the entire point of the WordPress Rest API. It gives you all the raw data and allows you to format that data in whatever means you like. That means, if I want to just display the post contents, let's say the title and the content, all I have to do is make this request, then grab the title rendered item and the content rendered item, put it into some sort of container in an HTML page or somewhere else, display it, and everything would work fine.
However, there's a caveat. If I want to display a full post, including the author name and a featured image and so on, I need more information than what I have here, or, rather, I need to request further information from the Rest API to get a complete picture, because right now all I have is the author ID and the featured media ID, and I have links to those objects, but I don't have the objects themselves. In the next movie, I'll show you how to get that information and show you how we're going to work with this throughout the rest of the course.
- 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