5.1 Registering a Custom Post Type and Taxonomy
In this lesson, you'll learn how custom post types and taxonomies are registered so you can use them in a custom query.
1.Introduction1 lesson, 00:58
2.WordPress and PHP 2 lessons, 20:34
3.First Steps With PHP3 lessons, 28:09
4.Coding in PHP6 lessons, 1:06:20
5.Practical Project: A Custom Archive Page2 lessons, 21:07
6.Conclusion1 lesson, 03:28
5.1 Registering a Custom Post Type and Taxonomy
Hello and welcome back to this Tuts+ course on learning PHP. In this part of the course, I'm gonna show you a real world example of how I created a theme template file and used some functions in a theme using different aspects of PHP that you've already learned. So in this, we'll use some conditionals. We'll use an advanced loop. And we'll use some arrays and variables. And I'll show you the files in my theme for this website that I used to create a bespoke archive page. So here's the archive page before making the adjustments. So this is an archive page for books because it's an author website. And this uses the same template as any other archive in the site. So for example, if we take a look at an archive here, this is using the same template file the archive.php file. It's slightly different at the moment because I've actually reverted to the archive.php file on this local demonstration site. And so you can see that the sidebar isn't showing correctly. But that will all be fixed as we work on the archive page. Because this page is designed to be full width rather than having a sidebar on the right-hand side. So at the moment, my archive page is pretty dull. It's designed for a layout where the featured images are landscape rather than being portrait, and where there's more text to the side. And also where there's a sidebar over here. So there's a lot of white space next to each book, which we don't want. What I want instead is for all the books to be next to each other, arranged in a grid. In addition, the books aren't shown in any specific order. This is just the order in which I added them to the site. It's not in publication order. So what I want to do instead is order them by series because a number of these books are in series or trilogies. So we're gonna create a custom query that is used in our archive page that uses the main query that WordPress runs, but runs through that query more than one time. So it only sends the query to the database once, which saves time and processing. But it reads through that query more than once in order to help put the books in a given series, one after the other. It'll also order them in the order we want them to. So let's just take a look on our dashboard. So here, we have our books. And you can see there's a list of them. And each of these books is within a series. Now you can learn more about registering custom post types in our Tuts+ course on custom post types. And, in addition, on the WordPress.org website. The site also uses a custom taxonomy, which again, you can learn about in one of our courses. This one covers both custom post types and taxonomies and on the WordPress.org Codex. But let me just walk you through the code to register that custom post type and taxonomy. Now these are both contained within functions in the functions.php file for our theme. Using the functions file for registering taxonomies and post types isn't necessarily best practice. Because if you change your theme, you don't want to lose those. In this case, I've included it within the theme, because the way that those post types are displayed is fundamental to the theme. So I would never use a theme without the ability to display those custom post types, as well as register them. And also, it's a theme that I developed for my own website rather than a third-party theme. But you could add this code to a plugin, and that would be the best thing to do if you're using a third-party theme. So let's take a look at the code. So to start with, we have a function for registering the post type. So here, this function tutsplus_register_post_type will contain the code for our post type. And so for any post type, you need to set up labels for it, which are what we'll see on the screen when we're working with that post type and also some arguments. So here we have labels which are the name of the post type, which is what will appear both in the browser window at the top of the screen and also in the menu. We've got the singular_name Book, add_new to New Book, add_new_item, Add New Book, edit_item, Edit Book, and so on. So we've got new_item, view_item, search_items. I'm gonna change that to Search Books instead of Search Book, not_found and not_found_in_trash. So those are the labels that we're setting up for our post type. And if I take you back to the website, you'll see up here, we've got Books. And here we've got Books and New Book, for example. So you can see how those labels come into the actual screen and the interface when you're working with your post type. And then we need to set up some arguments. And these relate to how the post type works. So firstly, we've got the labels, which is this labels variable up here. So you can see we're using variables, which we've already covered in this course. And we're using an array of values within that variable. So labels is this labels variable, has_archive is true. That's very important in this case because I want to display an archive of this post type, public is true, hierarchical is false. So it won't behave like pages, which are hierarchical. It will behave like posts, which are not hierarchical. And then here we have an array of things that it supports within WordPress. And these are largely related to the items that you'd add, the data that you'd add to it when editing that post or post of that post type and what you can save for it. So we've got a title, the editor, the excerpt, custom-fields, thumbnail or featured image, and page-attributes. There's an existing taxonomy of rmcc_book, and then category. And then the rewrite is a slug of books. And the reason I've done that is because by default, the URL for this would have in its slug, it would have tutsplus_book, which is the name of the new post type. I don't want that. That's not very user friendly and it's not very SEO friendly. So I use this rewrite argument to change the slug to books. So we can see up here when we look at it, it's rachelmclean/books, not rachelmclean/tutsplus_book. Now the reason I'm calling it tutsplus_ book is so that it's unique. It might be that I or somebody else might install a plugin in the future that has book as a post type in it. And I want to make sure that this is completely unique, so I'm using the tutsplus prefix to make that possible. We then have, moving down, the action hook that we hook that tutsplus_register_post_type function to. So this is our function up here, function tutsplus_register_post_type. And you'll remember we looked already in this course at functions and how you fire them using action hooks. And in this case, I'm hooking it to the init action hook. So that will make sure that my post type is active whenever I'm working in my WordPress site, or whenever I'm viewing it from the front end. So next, we're looking at the taxonomy. And taxonomies have some similar aspects to them when you're registering them as post types, but they're not exactly the same. So this taxonomy is called series. So again, it's got a name, a singular name. It's got values for searching, for all items, for editing. Updating, adding new items, the name of a new item, and the name in the menu. So if we go back to the admin pages for the site, you can see I've got series listed here with some values. And it's also listed under Books here because it's registered to the books post type. And you can see I have Add New Series, for example. So going back to our code, we've got those labels, and then again, we've got arguments. So the first argument is the labels using that labels variable, hierarchical is true. Now in this case, it means it will behave like categories, not like tags. Tags are not hierarchical. You can't nest tags underneath each other, but you can with categories. And a hierarchical taxonomy will behave like that. Sorting is true, I want it to be possible to sort these. I've got orderby term_order, and then I've got this rewrite argument again. And here, again, I've got a slug of series. So if I'm on an archive page for my series, it would have series as the slug and not tutsplus_series, which is the actual name of the taxonomy. I've got show_admin_column to be true, which is how we get, going back to the Books page here, I've got this admin column here. So if you want your taxonomy to show up on the listings for your posts, pages or post type in your admin screens, you need to use that show_admin_column argument. And I've got show_in_rest to be true. And what that does is adds it to the interface for Gutenberg because that uses the REST API. So when I'm editing one of my books, using the Gutenberg block editor, I can add a series to each book in the editing screen. Now if I didn't use that show_in_rest, it will be hidden to me if I was working in Gutenberg. Which, to be honest, is one of those things about Gutenberg which stumped me for quite a while until I discovered this argument when registering a taxonomy. So I had to go back to all my themes and plugins that had taxonomies registered and add that line. So then the final part of the function that I've created is the WordPress function register_taxonomy. And that has three parameters. Firstly, the taxonomy name, tutsplus_series, which is unique because of that prefix. The second is any post types that it might be registered to. So here I'm using that one post type, tutsplus_book, which I already registered. And then, finally, the arguments. And that's all of these arguments here. And then I hook it to the init hook again. So my tutsplus_register_taxonomy function is the second parameter of that add_action function. So that's how you register a taxonomy and a custom post type in WordPress. And that makes them fully functional in my admin screens. And while it creates my post type that I can use for an archive, it doesn't create my archive page. So in the next part of the course, that's what we'll do. We'll create a custom query using a lot of the PHP techniques that we've learned to output these books in a way that looks much better. See you next time, and thanks for watching.