Monday , 25 September 2017
Home / Programming / WordPress / Plugins / WordPress Taxonomies – WordPress Plugin Tutorial
WordPress Taxonomies
WordPress Taxonomies

WordPress Taxonomies – WordPress Plugin Tutorial

This post is part of the WordPress Plugin tutorial series. To find a link to all tutorials in order, then please click here.

Organization is important in WordPress. WordPress taxonomies allow us to organize our content. For example, our recipe plugin allows us to create recipes, but no way to search for certain recipes by origin. With WordPress taxonomies, we can easily do this.  The process is very similar to creating WordPress custom post types.

If you would like more information about WordPress taxonomies, click here. Basically, it allows you group things together like categories. We’ll be working on the recipe plugin we created earlier.

WordPress Taxonomies

Open up the recipe-post-type.php file and add this 1 line of code at the end of our jk_create_recipe_post_type() function.

We’re simply calling a function for us to create our taxonomies. Let’s create this function. Under our jk_create_recipe_post_type() function, add this bit of code.

Creating a taxonomy is a very similar process to creating WordPress post types. We create an array called labels. It contains the same items like the last label we created. I just copy and pasted this in our function. I just replaced Recipe/Recipes with Origin.

Next, our $taxonomy variable contains an array. This will contain our arguments for our taxonomy. We’ll keep it basic. We already discussed labels in the last tutorial, same thing applies here. Everything is pretty much the same, but what we haven’t discussed is hierarchical. This is a true of false value to whether or not you want a hierarchical in your taxonomy.

Lastly, our function register_taxonomy has 3 parameters. The first parameter is the name of your taxonomy. The second parameter accepts which post type this taxonomy belongs to. In our case, we put in recipes. This value should correspond to the value in the first parameter of the register_post_type() function. The last parameter takes in our arguments. We pass in the $taxonomy array.

That’s it! Save your work and refresh your WordPress dashboard. You should see your taxonomy under your custom post type in the menu. Click on it and you should see this.

WordPress Taxonomies
WordPress Taxonomies

Notice that I’ve already added some origins already. I’ve even added Paris as a sub origin of France. This is the effect of the hierarchical argument in the $taxonomy array.

Conclusion

That’s all there is to taxonomies. We’re going to finish our plugin with Metadata. We’ve already discussed how to create meta boxes, but we never learned how to put it to good use and how it can benefit custom post types.

About Jasko Koyn

Check Also

WordPress Widgets API

Widgets API – WordPress Plugin Tutorial

The widgets API is what we’ll be learning next. It’s very simple to use. It …

One comment

  1. Congratulations, your tutorial has been very useful.

    In this part however you seem to have forgotten to show the add_action() call where the taxonomies() function is hooked. Following your style, the following code should go in the index.php of the plugin in the action hooks section:

    add_action(‘init’, ‘taxonomies’);

Leave a Reply

Your email address will not be published. Required fields are marked *

ULTIMATE WORDPRESS DEVELOPMENT COURSE
Want to learn how to create your own custom WordPress plugins and themes? Check out my course!
Get 93% off my course. You'll also learn how to integrate WooCommerce and BuddyPress into your projects. This is a limited time offer
Yes, I would like to get 93% off your course!
No Thanks!
close-link