Creating a Custom Taxonomy

Posted: July 2, 2010 in PHP
Tags: , , , ,

Editing your theme’s functions.php file

WordPress version 3 does not allow you to create custom taxonomies from the administration screen. To initially define your custom taxonomies without a plugin, you’ll need to add a little bit of code to your theme’s functions.php file. This isn’t too difficult — just follow my lead.

To add custom taxonomies, we need to edit the “functions.php” file found inside your theme directory. Location of functions.php :  [website_root]/wp/wp-content/themes/[your_theme]/functions.php

Adding the Taxonomies in Code

  1. One Function to Create Them All

    First, we need to build one function that creates all the taxonomies we need. We’ll call the function “build_taxonomies.” Let’s add this function to the bottom of the functions.php file.

    <?php
       function build_taxonomies() {
    	// code will go here
    	}
    ?>
    
  2. Defining the Taxonomies

    Next, for each taxonomy we want to create, we need to call a particular WordPress function with the right parameters. Here’s the function, and its important parameters explained.

    <?php
      register_taxonomy(
        'internal_name',
        'object_type',
        array(
          'hierarchical' => {true|false},
          'label' => 'Human Readable Name',
          'query_var' => {true|false},
          'rewrite' => {true|false}
        )
     );
    ?>
    
    • internal_name: What will this taxonomy be called from inside WordPress, in the database and template files?
    • object_type: Which types of content can be classified with this taxonomy? Possible values are “post, page, link,” and then names of custom post types we’ll learn to create in a future tutorial.
    • Next comes an array of optional parameters. We’ll use the most important ones here in this tutorial, but a full list can be found on the Function Reference / register_taxonomy Codex page. The parameters we’ll use are:
    • hierarchical: If ‘true,’ this taxonomy has hierarchical abilities like WordPress Categories. If ‘false,’ this taxonomy behaves much like freeform Tags.
    • label: This is the human-readable name used in your site’s interface to label the taxonomy.
    • query_var: If ‘true,’ we’ll be able to ask WordPress for posts dependent upon the selections for this taxonomy. For example, we could search for all the posts where the operating system taxonomy has ‘Windows’ selected.
    • rewrite: If ‘true,’ WordPress will use friendly URL’s when viewing a page for this taxonomy. For example, a page listing all the posts with the “Windows” operating system selected would be represented by the following url: http://domain.com/operating_system/windowsOur entry specific to adding the Operating System taxonomy looks like so:
      <?php
      register_taxonomy( 'operating_system', 'post', array( 'hierarchical' => true,
      'label' => 'Operating System',
      'query_var' => true,
      'rewrite' => true )
      );
      ?>
      
  3. Calling the Taxonomy-Creating Function

    We need to add one more line to the “functions.php” file so our “build_taxonomies” function will actually be executed. We’ll “hook” the “build_taxonomies” function to the “init” event by adding the following code:

    add_action( 'init', 'build_taxonomies', 0 );
    

    You can add this line anywhere, but I generally add it above the function we’re calling, so it would look like this:

    <?php
        // Custom Taxonomy Code
        add_action( 'init', 'build_taxonomies', 0 );
    
        function build_taxonomies() {
          register_taxonomy( 'operating_system', 'post', array( 'hierarchical' => true, 'label' => 'Operating System', 'query_var' => true, 'rewrite' => true ) );
        }
    ?>
    
More Information

register_texonomy

Hope it will help to all. Try this on your blog.

Advertisements
Comments
  1. Brian Yerkes says:

    Thanks for the post. I’ve got a custom taxonomy working within a custom post type….but I’m struggling with the query on the frontend…
    Here’s what I’ve got:

    $loop = new WP_Query( array( ‘post_type’ => ‘listing’, ‘posts_per_page’ => 3) );

    I need that line to include a taxonomy…Here’s the custom taxonomy I created:

    register_taxonomy(“speaker”, array(“listing”), array(“hierarchical” => true, “label” => “Property Category”, “singular_label” => “Property Category”, “rewrite” => true));

    When I add a new category to this custom taxonomy, how do I call it? It looks like stores it as a tag_ID when I try to edit it, but that doesn’t work when I call it on the front end.

    Any help is appreciated, thanks

    • chinmoy29 says:

      @Brain

      How to get the taxonomy content from the post? Am I right? I am giving you some code, hope it will help you.


      $property_cat_list = get_the_term_list( $post->ID, 'speaker', 'Property Category(s): ', ', ', '' );

      if ( '' != $property_cat_list ) {
      $property_cat_text = "$property_cat_list
      \n";

      echo $property_cat_text ;
      }

  2. chinmoy29 says:

    $term = get_term_by( ‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ) );

    This gets all of the information about the taxonomy that called this page and returns it as an object into the variable $term. For example, the “Mac OS” classification returns an object as such:

    print_r($term);

    output:

    stdClass Object
    (
    [term_id] => 13
    [name] => Mac OS
    [slug] => mac-os
    [term_group] => 0
    [term_taxonomy_id] => 22
    [taxonomy] => operating_system
    [description] =>
    [parent] => 0
    [count] => 2
    )

  3. Viktor says:

    Hey,

    Thanks for your help. I was able to figure it out. After I used is_tax to find the right content and use $term from above to get the tax name, I managed to get my taxonomy.php to work.

    After that it is REQUIRED (I think) to go re-save your permalinks, which rebuilds all of them. This makes WP fix custom taxonomy links, it works now. You should mention it in your post for people like me :-p

    Question. Right now it writes links as (from your example) /operating_system/windows
    How can I remove operating_system and make it website.com/windows?

    Thanks, appreciate your help!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s