Building Custom Taxonomies

Now it's time to build your first custom taxonomy! As an example you are going to create a simple taxonomy for defining ingredients. Imagine you are a food blogger who likes to post recipes online. You are going to set up a custom taxonomy to define each type of ingredient in the recipes you post.

The first thing you are going to do is define your new taxonomy using the register_taxonomy WordPress function. This function allows you to customize how your new taxonomy will work and look. The following code would work in a custom plugin, but for this example you'll use the functions.php file in your theme folder. Open up functions.php in your theme and add the following code:

<?php add_action( 'init', 'define_ingredient_taxonomy', 0 );

function define_ingredient_taxonomy() {

register_taxonomy( 'ingredients', 'post', array( 'hierarchical' => false, 'label' => 'Ingredients', 'query_var' => true, 'rewrite' => true ) );

The first thing you do is call the init hook, which tells WordPress to execute your def ine_ingredient_ taxonomy function during initialization. Your function then calls the WordPress function register_taxonomy. This function is used to create your custom taxonomy based on what values you send.

Let's break down the parameters you are sending to the register_taxonomy function. The first parameter is the taxonomy name, in this case ' ingredients '. This is the name that will define this taxonomy in the database. The second parameter is the object type. For this example, you will use 'post', but you can also create custom taxonomies for pages and links as well. The third and final parameter is for arguments, meaning you actually send multiple values to this parameter.

Four arguments can be sent to the argument parameter. The first is hierarchical, which defines whether or not your custom taxonomy will contain hierarchy. In the preceding example you set this to false, so your taxonomy will function just like tags. The next argument, label, is used to set the name of your taxonomy for use in admin pages within WordPress. The query_var argument is used to set whether custom query posts should be allowed against this taxonomy. You see an example of this feature later in the chapter.

The final argument is for rewrite, which you set to true. This tells WordPress whether or not you want a pretty permalink when viewing your custom taxonomy. By setting this to true you can access your custom taxonomy posts like rather than the ugly method of

Now that you have created your custom taxonomy for ingredients, take a look at what WordPress has done with your new taxonomy. The first thing you will notice on your admin dashboard is a new link under the Posts menu for your taxonomy labeled ingredients, as shown in Figure 6-4.

Clicking this new menu item brings you to the custom taxonomy admin panel for ingredients, shown in Figure 6-5. This admin panel works exactly as the post tags admin panel does. Here you can create new taxonomy terms, edit and delete existing terms, find how many posts are assigned to each, and also search taxonomy terms.


Custom taxonomy menu option


Custom taxonomy menu option

FIGURE 6-5: Custom taxonomy admin panel

The final new item added for your custom taxonomy is a meta box on the post edit screen, shown in Figure 6-6. To view this, click Add New Post. The meta box should appear on the right-hand side of your screen, and look very similar to the Post Tags meta box. Here you can easily add and delete new ingredients on your posts.

FIGURE 6-6: Custom taxonomy meta box

Remember that these features will not display if you are using hierarchy in your custom taxonomy. Also, this only works on posts, but pages and links will be added in a future release.

Starting with WordPress 2.9 you have access to the meta box functions. This means you can add post-specific meta boxes to other areas of WordPress, such as pages. Following is an example showing how to add a custom taxonomy term meta box to your pages:

<?php register_taxonomy( 'ingredients', 'page', array ( 'hierarchical' => false, 'label' => 'Ingredients', 'query_var' => true, 'rewrite' => true ) );

function tax_page_meta_box() {

foreach ( get_object_taxonomies( 'page' ) as $taxes ) { $mytax = get_taxonomy( $taxes );

add_meta_box( "tag-ings", $mytax->label, 'post_tags_meta_box' 'page', 'side', 'core' );

First you define your new custom taxonomy name: ingredients. You also set your taxonomy type to page. Currently hierarchy taxonomies are not supported using this method, so make sure you set hierarchical to false. Next you use the admin_menu hook to call your function, tax_page_meta_box. Finally, the add_meta_box function is called to add the meta box to your page screen. This example is a little more complex, but it shows the power of custom taxonomies and how you can use that power for more than just your posts.

Was this article helpful?

0 0

Post a comment