1. Home
  2. Docs
  3. WordPress Developer
  4. Create A Custom Taxonomy In WordPress

Create A Custom Taxonomy In WordPress

Taxonomy is a way to group things together.

Default Taxonomies

  1. Categories (hierarchal),
  2. Tags (multifaceted),
  3. Links (multifaceted),
  4. Navigation menu (hierarchal).

WordPress has four built in taxonomies.

Category

The ‘category‘ taxonomy lets you group posts together by sorting them into various categories. These categories can then be seen on the site by using ‘/category/name‘ types of URLs. Categories tend to be pre-defined and broad ranging.

Tag

The ‘post_tag‘ taxonomy is similar to categories, but more free form. Tags can be made up on the fly, by simply typing them in. They can be seen on the site in the ‘/tag/name‘ types of URLs. Posts tend to have numerous tags, and they are generally displayed near posts or in the form of tag clouds.

Link Category

The ‘link_category‘ taxonomy lets you categorize your links. These tend to be used only internally, for organizational reasons, and are not usually exposed on the site itself. They are handy for defining groups of links to be displayed in sidebars and the like.

Post Formats

The ‘post_format‘ taxonomy was introduced in WordPress 3.1 and it is a piece of meta information that can be used by a theme to customize its presentation of a post. New post formats can’t be created or added to the default existing ones.

Custom Taxonomies

WordPress provides a new method of grouping content by allowing you to create your own custom taxonomies.

In WordPress, you can create or register a new taxonomy by using the register_taxonomy() function.

Here’s an example of registering a “coupon” taxonomy:

function coupon_init() {
    // create a new taxonomy
    register_taxonomy(
        'coupon',
        'post',
        array(
                'labels' => array(
                    'name' => __( 'Coupons', 'taxonomy general name' ),
                    'singular_name' => __( 'Coupon', 'taxonomy singular name' ),
                    'search_items' =>  __( 'Search Coupons' ),
                    'all_items' => __( 'All Coupons' ),
                    'parent_item' => __( 'Parent Coupons' ),
                    'parent_item_colon' => __( 'Parent Coupon:' ),
                    'edit_item' => __( 'Edit Coupon' ),
                    'update_item' => __( 'Update Coupon' ),
                    'add_new_item' => __( 'Add New Coupon' ),
                    'new_item_name' => __( 'New Coupon Name' ),
                    'menu_name' => __( 'Coupons' ),
            ),
            'rewrite' => array( 'slug' => 'coupon' ),
            'hierarchical' => true,
        )
    );
}
add_action( 'init', 'coupon_init' );

You can add above code in theme functions.php file or in plugin file.

Here, the “coupon” taxonomy is defined. It’s defined to work for posts, and a rewrite slug is defined to make the url into ‘/coupon/’ instead of ‘/coupon/’. The capabilities line is optional.

After adding a few terms to your new taxonomy, you can begin to organize the content in your posts by Coupon. A new “Coupons” box will appear to the right of your posts in the WordPress admin area. Use this the way you would categories.

Using taxonomy

Once you’ve added a taxonomy, you’ll find that WordPress creates a new meta box on posts for you. This new meta box looks almost exactly like the Tags box and will let you add tags to those posts.

When you add a custom taxonomy to a WordPress theme, you can display its content using one of WordPress’ taxonomy theme templates.

  • taxonomy-{taxonomy}-{slug}.php We could use this to create a theme template for a particular location, such as taxonomy-location-boston.php for the term “boston.”
  • taxonomy-{taxonomy}.php If the taxonomy were location, WordPress would look for taxonomy-location.php.
  • taxonomy.php This template is used for all custom taxonomies.
  • archive.php If no taxonomy-specific template is found, then the taxonomy that lists pages will use the archive template.
  • index.php If no other template is found, then this will be used.

To add terms to an object using your taxonomy, you’ll need to use the wp_set_object_terms() function.

Listing the terms

If you want to have a custom list in your theme, then you can pass the taxonomy name into the the_terms() function in the Loop, like so:

the_terms( $post->ID, 'coupon', 'Coupon: ', ', ', ' ' );

To get a dynamic list of the terms as an array for use in content, use get_terms(). For example, to list all the terms of a custom taxonomy using check boxes:

 $terms = get_terms( array(
                          'taxonomy' => 'your_custom_taxonomy',
                          'hide_empty' => false,  ) );

 $output = '';
 foreach($terms as $term){
    $output .= '<input type="checkbox" name="terms" value="' . $term->name . '" /> ' .  $term->name . '<br />';
  }

A tag cloud provides a great way for users to browse content. The wp_tag_cloud() function makes creating a tag cloud with a custom taxonomy easy.

Most navigation systems begin with an unordered list. You can generate an unordered list of links to taxonomy archive pages using the wp_list_categories() function.

Was this article helpful to you? Yes No

How can we help?