Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:7Length:39 minutes
Custom post types in wordpress 400x277
  • Overview
  • Transcript

2.2 Complete the Custom Post Type Registration

In this lesson, we’ll finalize the code to register the post type.

Related Links

2.2 Complete the Custom Post Type Registration

Hello and welcome back to this Tuts+ course, on working with custom post types in Wordpress. In this part of the course, we're going to finish our plugin to register our post type. So, you'll see that what we have so far is a function here, Tuts+ register post type, that's incomplete. And let me show you whether that's made any changes to the site. So here's my plugin screen and my plugin is activated, but no custom post type is appearing over here. And that's because I haven't finished my plugin. So there are no arguments at the moment. It's just an empty array, so this isn't a thing for what WordPress to do. So, let's correct that. So when we're using the arguments here, there is an array of arguments that you use with this register post type function. And we'll start with the labels. So labels, instead of typing them all out here as an array within our array. We're just using this variable that we've already defined as the array of labels above. And I'll add some more arguments and then work through them with you and show you what they do. So let's just pause to take a look at that now. So our arguments include the labels, which we've already defined here using this variable has archive. So that defines whether or post type has its own archive page that you can access via the front end of WordPress. Now in this case, I definitely want that to be true. I want to be able to create an archive page for all my projects, and later in the course we'll be creating a custom archive template for that. Public shows whether it's visible on the front end and whether it has pages that are public which defaults to true so you could leave that out. Hierarchical can either be false or true and you may notice that I thought about that one when I was doing it. Now a hierarchical post type will behave like a page in WordPress. So you'll be able to create a structure for your post type. And non hierarchical one where I've set up false like this, will create something a bit like a post in WordPress. And posts can be arranged hierarchically in the same way that pages can. And that's what I want to happen for my projects. The rewrite argument defines what appears in the browser as the URL when somebody visits the project post type. Now, the name of my post type is tutsplus_project. And by default, that would be what appears. But I don't want that, it doesn't look very neat in my URLs. So I'm changing the slug to projects. And then we have all the things that it supports within WordPress, which I'll go on and create now. But finally, the taxonomies that this post type applies to. And I've isolated the post tag, commonly known as the tag, and the category. Now you could change this, you could create a custom taxonomy, an agile post type to that instead, or you could just use either the post tag or the category. It's up to you. So now let's type in what it supports. So this post type will support these elements of the post, which you will see in the editing screen. So a title for the post, the editor for the post content, an excerpt, custom fields, a thumbnail featured image and page attributes. And you can include whichever of these you need to be able to use your post as you want to. Now my posts will behave a lot like a standard post. So I'm including all of those. So I just need to correct an error that I just spotted. That post tag should be inside the inverted commas and not with the comma. After it, well the comma should be after the closing inverted comma. And then finally, I need to add an additional argument which is showing rest. And what that will do is make this post type available to the rest API which makes it available with the Gutenberg editor. So that needs to be set to true because the default is false and it won't display if you're using the Gutenberg editor. So I'm gonna save that and then take a look at my projects in my site. So now, here you can see over on the left hand side, we have projects. So the label is projects, if I want to write a new one, I've got Add New Project, which is that label that I set up. And it's got Categories and Tags as the taxonomies that apply to it. So let's just click on projects and you can see again here we can Add New Project, which is one of the labels that I added. And I haven't any new projects added at the moment. So I'll go ahead and add some. And then I can show you how to display them in your site. So our plugin is now complete and we have our post type registered. In the next part of the course, we'll take some new projects and I'll show you how to display them in the feed with your blog posts. See you next time and thanks for watching.

Back to the top