Join Morten Rand-Hendriksen for an in-depth discussion in this video Add custom post types and taxonomies to the REST API, part of WordPress: REST API.
- [Narrator] Custom post types and taxonomies are pretty much expected additions any time you build an advanced website or solution with WordPress. And now that we have the REST API, we need to make sure those custom post types and taxonomies are accessible through the API. The good news is you don't need to write any convoluted custom code to do so. REST API support for both custom post types and taxonomies is enabled in their original registration. The WordPress developer handbook provides all the info we need, both on the Adding REST API Support For Custom Content Types page and directly in the pages for register_post_type and register_taxonomy.
For both register_post_type and register_taxonomy, there are three relevant arguments. show_in_rest, which takes a Boolean value of true or false and indicates whether the post type or taxonomy should be available through the REST API. rest_base, which allows you to change the post type or taxonomy name at the end of the route. The default is the post type or taxonomy name. And finally, rest_controller_class, which allows you to create a custom controller class for the post type or taxonomy.
All this means if you just want to add your custom post type or taxonomy to the REST API without any fancy trimmings, simply set show_in_rest to true and you're good to go. That really all there is to it. To show you exactly how this is done and see what happens to the REST API response, I've created a custom post type plugin you can find at exercise files for this movie, 0302. That plugin is called Morten's Custom Post Types and Taxonomies, once you add it into your site and activate it, you'll see a new post type appears called Lectures and within the Lectures post type, you have two new Taxonomies, Classes and Years.
To see the output of this new custom post type and its taxonomies in the REST API it's a good idea to add something to it, so go to the Lectures and just add a lecture, I've added the most bare bones lecture possible, it just says First lecture, This is the first lecture and then I've given it a Class WP REST API 101 and the year 2017. Looking at the code for this custom post type plugin called cpt.php and placed in the plugins folder in my local install of WordPress, you'll see here in the definition for the custom post type I've commented out show_in_rest, so right now it's not being shown in the REST API output.
Scrolling down, you'll find a definition for each of the two Taxonomies and again, I've commented out show_in_rest here and down here. That means right now, if I go to Postman and go to Post Types and then run the request, we have the expected post types we always have with any WordPress site, post, page and attachment. That's it, now let's what happens if I set show_in_rest to true for the custom post type. Save cpt.php, go back and just rerun the request for types and now in addition to posts, page, attachment I have lectures, and because lectures has its capability types set to post the new route for lectures inherits all the properties of the regular post.
So here we have all the different properties and functionalities you expect from the post item, meaning you can access this exactly as you would the regular post. It also means I can now request only lectures in the exact same way I would request regular posts. So if I copy this, open a new tab and at the end of this route instead of saying posts which would give me the 10 most recent posts, I'm going to say lectures. Send that and we get the lecture I created, First lecture, This is the first lecture and as we scroll down, you'll see it has all the information you would expect from a post object.
But when we scroll down to the wp:term section, you'll also see we currently only have category and tag as the taxonomies. That's an easy fix, go back into cpt.php and find the definition for each of the taxonomies. Here we have the first one and the second one. Save the change, go back and just rerun this request to the API, and now when I scroll down under wp:term we nave have category and tag just like before and then we have the new custom taxonomies, class and year.
So like I said, adding REST API support for your custom post types and taxonomies is as easy as setting show_in_rest to true.
- 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