So far we've created a single custom taxonomy called Meal type that's applied to all three custom post types and this taxonomy is hierarchical. You can see it displays down here as hierarchical => true, and when we go to WordPress Admin, it displays as a regular category. But in WordPress we've two types of taxonomies. We have the hierarchical taxonomies like the categories, or in our case Meal type, and we also have the nonhierarchical ones like tags. Now I want to introduce a nonhierarchical taxonomy.
You would think that the simplest way to do it would be to simply copy our Meal type taxonomy and just paste it in and change the labels and the name. But that nonhierarchical taxonomy is slightly different from the hierarchical one and we have to do it exactly right to make it work. Instead of doing the copy and paste, let's go to the code snippets and copy it from there. You'll see under 04.05 we have the complete stack for the Ingredients taxonomy. So simply copy it all the way down to the end bracket here.
Go back into posttypes.php, under my cooking_create_taxonomies function, and the time I'll place the new variables inside that function. That's because we only run one function to create all our taxonomies. What I mean by inside is it has to be placed before this end curly bracket at the bottom. So I'll create a few more lines and then paste in my new Ingredients taxonomy. You'll see when we read it from the top, that it looks pretty much the same. We have commented out Ingredients, so I know what it is, and then we have the name, the singular_name, search_ items and so on and so forth, all the way down.
The change happens at the bottom here. First of all, the hierarchical is set to false this time, which means it's a nonhierarchical type of taxonomy. And more importantly, down here we've this new variable called update_count_callback. What this variable does is it helps WordPress count how many items are listed under a certain taxonomy, and also how many taxonomies are listed. This is extremely important if you have a large site, because otherwise WordPress will have a really hard time finding all the items that are related to a certain taxonomy, and it will start acting really sluggish.
Simply adding this variable, update_count_callback, with its value, update_post_term_count, will make all the difference here. So you should always put it in. And this only applies to a nonhierarchical post types. It will work fine even if you don't put it in, but you'll see performance issues, which is why it's so important. When I now save this and go back to WordPress Admin, you'll see whereas before we only have the Meal type on the side here, when I reload it we have both Meal type, which is a hierarchical or category type taxonomy, and we also have Ingredients which is a nonhierarchical taxonomy.
And because we added all our labels, you'll see it says Ingredients, Separate ingredients with commas, Choose from the most used ingredients, and so on. Now I just want to show you what happens if you make a mistake here, because it can be quite jarring and I just want to show you that it's not as dangerous as it seems. If I make a simple error somewhere in this, for instance, I can take out one of these apostrophes and I save it, it has quite jarring consequences. When I try to reload the page, I simply get a server error because I've broken the functions file in my theme, and this is disturbing when you first experience it, because you are like, oh my God! I just broke the site.
The reality is you didn't break the site; you just made a file that WordPress can't read. So all you have to do is reintroduce that apostrophe, save it, reload Admin, and you're back up and running. The same goes if you take this entire stack and place it outside of the curly bracket. You'll get the same result. So in general what I recommend is when you're working with custom post types or custom taxonomies, anything in the functions file or the post types file, make incremental changes at a time, so that if your site tanks, then you know that it must have happened in that last change you did and then you can revert back to what you had before.
That's also why I've provided you with a complete code for all these post types, so that you can just paste them in and you know it works. What I'm talking about is if you go back to the code-snippets file and scroll down, you'll see it says Full function with all seven custom taxonomies for posttypes.php: So if you copy this from Add custom taxonomies all the way down, and there is a lot of code here, to the end curly bracket, you can then go to our function and highlight it from the curly bracket, all the way up to where it starts, delete it, and then paste in the code we just copied.
Save the posttypes.php file and reload our Admin Panel and you'll see now our Recipes have five custom taxonomies. Meal type, Level of difficulty, Ingredients, Preparation time, and Servings. Photos have Meal type, Ingredients, and Techniques, and Videos has Meal type, Ingredients and Video types, and all these taxonomies will then work. This also means that if you're working on some other project, like something cars or skis or whatever, you can simply take these custom taxonomies that we've built inside our posttypes.php file, copy them, simply change the names and labels, and then you have your own custom taxonomies customized specifically to your needs.
It really is as simple as going in, changing the names. Video type can be changed to anything else and then it becomes a custom taxonomy just for you. When making nonhierarchical custom taxonomies, it's important to remember to add all the extra elements to avoid database trouble down the road. For this reason, I suggest you keep two separate snippets of your code handy. One for hierarchical taxonomies and one for nonhierarchical ones. That way you'll never forget.
Get unlimited access to all courses for just $25/month.Become a member
117 Video lessons · 43219 Viewers
119 Video lessons · 54459 Viewers
65 Video lessons · 14665 Viewers
113 Video lessons · 82959 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Your file was successfully uploaded.