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.
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.
$labels = array(
'name' => 'Origin',
'singular_name' => 'Origin',
'add_new' => 'Add New Origin',
'add_new_item' => 'Add New Origin',
'edit' => 'Edit',
'edit_item' => 'Edit Origin Review',
'new_item' => 'New Origin',
'view' => 'View',
'view_item' => 'View Origin',
'searh_items' => 'Search Origins',
'not_found' => 'No Recipes found',
'not_found_in_trash' => 'No Recipes found in trash',
'parent' => 'Parent Recipe'
$taxonomy = array(
'labels' => $labels,
'hierarchical' => true,
'query_var' => 'recipe_origin',
'rewrite' => array ( 'slug' => 'recipes/origin')
register_taxonomy( 'Origin', 'recipes', $taxonomy);
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.
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.
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.