1.2 Registering Custom Post Types and Taxonomies in WordPress
1.Registering Custom Post Types and Taxonomies in WordPress2 lessons, 11:03
1.2 Registering Custom Post Types and Taxonomies in WordPress
[MUSIC] Hello and welcome to this Tax Plus coffee break course on registering post types and taxonomies in WordPress. Being able to create your own post types and taxonomies is incredibly useful and it's what turns WordPress from a simple broking platform into a fully featured content management system or CMS, so let's see how to do it. The best way to register a post or taxonomy is to write a plugin to do it. Now theoretically you could do this in the functions found in your frame, but the problem with doing that is that if you should ever change your frame in the future you lose access to your post types. So it's much better to do it via a plugin. So let's create a plugin to do that. Here's the plugins that I've got running on my site, and I've created an empty plugin plus register post types and taxonomies and you can see that I've added the opening comments to tell WordPress about the program. So I'll need to do two things. Firstly I'll register a postcard, and secondly a taxonomy. Now I'm going to imagine I'm working on a portfolio site and I want to add some projects that I can display to people as portfolio projects and a project type taxonomy for different types of projects to display. So go ahead and create the plugin and activate it on your site. Now let's create the function to register the post time. Now this function needs to be hooked to the in it took so I'll add that using AD action. Now inside my function, I need to do three things. I need to define the labels for my post type, that users will see when they're adding and editing posts about post type. I need to define some arguments for my post type, and then I need to register the post type. So let's start with the labels. So these go in an array and that's how those AMP. So firstly, there's the name of my post type. And that's in the plural. And next I add some more labels. And I'm gonna do that by copying and pasting this particular row and then editing as needs be. So the next one is the singular name. And that's project, and you can see I've got this translation ready. Now I need the text for the add new button. Then for the menu. Throughout and you again then for editing. Then for a new item. And that appears on the project page when I want to add a new one. Then for viewing the item. Then for searching. And that's in the plural. Then I need the message when an item is not found and then finally the message when no items are found in the trash. So those are the labels for my post type. Next I define the arguments, which include those labels. The first argument in my array is for the labels. Next, I use the has_archive argument which I want to be true because I want to show archive pages for this post type The next one is whether this is public, Which again I want to be true. The next is whether I want my post tag to be hierarchical In the same way the pages are as against posts and in this case, I'm gonna put false. And then my next argument is what my post type supports and that's another array of arguments. So my post type will support a title, the text editor, the excerpt, custom fails, thumbnails, featured images and page attributes. So those are my arguments. The final thing is to use the register post type function to actually register my post type. And the ridge to post up function has two parameters, the first is the unique ID of the post type and then the second is the arguments. And I can say, I got a typo and that's all just correct. Now the ID should be in the singular. And then the arguments is defined using my args. So that's my function to register the post type. If I save that and go back to my site, you'll see my post type displayed in the admin screens. And there it is, over here in the admin menu, I've got projects. So now let's go on to out the taxonomy for our projects. So back in my plug in file I'll create another function. And again I'll hook it to the init hook. So this works in a very similar way. First I'll define some labels, and then some arguments, and then I'll use the register taxonomy function to actually register my taxonomy So those are my labels. You can see I've got a label for the name, the singular name, for searching, all items, editing items and updating them, adding new ones with the name and also the menu name. And if you want to see details of the arguments you can use for labels for post-op syntax on the maze, take a look at the relevant pages in the codex. Which I've given you links to with this course. And you can save them here, register post type image to taxonomy. So now let's go back to our function and the next thing is to add my arguments. So those are my arguments that includes the labels, a hierarchical structure, details for sorting, and also sharing an admin column which is important to show you taxonomy terms In the list of posts. And then finally I'll use the register taxonomy function to register my taxonomy. And this has three parameters this time, the ID of the taxonomy, the post type that it relates to and that can be more than one you can put those in the array, and the arguments. And that's my function to reach the taxonomy. So I'll save that and go back to my site. So let's just have a quick look at the code before saving. And I've made a mistake here. I've missed off the arrow on my equals here. So, I fixed all those and I'll save. And now, if I go back into the site, you can see I've got Projects and I've got Project Types. So, if I click on that I can add as many project types as I want and I can also add a New Project in which I have access to the Project Types taxonomy. So, that's how you add a custom post type and a custom taxonomy in WordPress. Once you start out the more of these, you'll find that you can create a site that works in exactly the way you want to, and has a completely custom structure and content enjoy using WordPress as a CMS and I hope you found this course useful.