WordPress ships with a set of default content types that you might be very used to Posts, Media, Pages, and Comments. In many cases, we'll want custom content types that fit our use-case. To do this, we'll create custom post types.
In this lesson, we will use a plugin, Custom Post Type UI, to create a custom post type and see how to retrieve the data in the REST API.
Instructor: [0:00] By default, WordPress comes with a number of content, type posts, media, pages, and comments. Often though, we're using WordPress not just to manage a blog, and we want the custom functionality. To get this functionality, we create custom post types, even when it's not a post, so it might be for books, students, teachers. It could be for anything.
[0:18] We're going to use a plugin to be able to create a custom post type. One of the exciting things about WordPress is that it has a large plugin library. People have been building functionality into WordPress for years. A lot of these plugins are published in the plugin store.
[0:33] Here, I have access to four plugins that are featured, popular, recommended, and favorites. Because we're building a headless WordPress instance, we're not going to use all of the plugins that would normally be set up. The one we're going to start with is called custom post type UI.
[0:49] We can search for it, and we can install it. Installing it downloads the files to our filebase, and then we need to activate it, so that our instance knows it's allowed to use them.
[0:58] Different plugins do different things. This one adds a new menu item over here. Some add many sub-items or just change in how certain things behave. We got this [inaudible] . We don't really need it. We're going to add a custom post type. You can do this through custom code, but this way, it's easier to manage.
[1:14] If we imagine that this is an e-commerce store selling some furniture, let's have a custom post type that just deals with chairs. Our plural is chairs. Our singular is chair. We will add our post type.
[1:26] You can see there's a lot of functionality that we can add, a lot of settings that we can play with. Here we can see, show in REST API has been set in True, and show in UI has been set in True. I can see Chairs here. I've got All Chairs, add a new chair.
[1:40] I can see that it should be available in my REST API. Let's go into Chairs and add a new chair. At the moment, this isn't behaving any differently from a normal post. We have access to all the same blocks we had access to before.
[1:54] Because our back end is saying this is a chair, we can explicitly style this, or expect certain data to be present on this post that wouldn't necessarily be present on others. Office chair. I like my office chair.
[2:11] Now, let's check out our JSON. In Chairs, we have got our office chair. It's got a slug. It's got a type. It's got a title, and it has the data that we've attached to it. This becomes even more powerful when we're able to add custom data that's not just a rendered HTML.