WordPress comes with inbuilt functionality which enables you to group posts or post types together. This functionality is called a taxonomy, by default WordPress comes with two default types, there is categories and tags. The difference between a category and a tag is that categories can be grouped in a hierarchy so you can have parent categories. Tags give you more freedom then categories you can add new tags easily by typing them in the tags textbox on the post screen. Normally a post will have multiple tags but will only one category. In a normal blog website having just categories and tags is enough functionality to group your posts, but WordPress is a CMS and can be used to create custom post types. These new post types might not fit into the previous taxonomies you have used, so you might have to create a new taxonomy to allow you to group your new post types. Since WordPress 2.3 developers have been able to create their own custom taxonomies by using the function register_taxonomy(). This function will take 3 parameters, the name of the taxonomy, the post type to assign the taxonomy and arguments to define the taxonomy.
<?php
add_action( 'init', 'register_new_taxonomy' );
function register_new_taxonomy() {
register_taxonomy( $taxonomy, $object_type, $args );
}
?>
The arguments that you can define for the taxonomy are: - label - The plural descriptive name for the taxonomy.
Below is a basic example of how you can use register_taxonomy() to create your own custom taxonomy.
add_action( 'init', 'register_new_taxonomy' );
function register_new_taxonomy() {
$labels = array(
'name' => _x( 'plural taxonomy', 'Taxonomy general name', 'text_domain' ),
'singular_name' => _x( 'single taxonomy', 'Taxonomy singular name', 'text_domain' ),
'search_items' => __( 'Search plural taxonomy', 'text_domain' ),
'popular_items' => __( 'Popular plural taxonomy', 'text_domain' ),
'all_items' => __( 'All plural taxonomy', 'text_domain' ),
'parent_item' => __( 'Parent single taxonomy', 'text_domain' ),
'parent_item_colon' => __( 'Parent single taxonomy', 'text_domain' ),
'edit_item' => __( 'Edit single taxonomy', 'text_domain' ),
'update_item' => __( 'Update single taxonomy', 'text_domain' ),
'add_new_item' => __( 'Add New single taxonomy', 'text_domain' ),
'new_item_name' => __( 'New single taxonomy Name', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove plural taxonomy', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from most used plural taxonomy', 'text_domain' ),
'menu_name' => __( 'single taxonomy', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'hierarchical' => true,
'show_tagcloud' => true,
'show_ui' => true,
'rewrite' => true,
'query_var' => true,
'show_admin_column' => false,
'capabilities' => '',
);
register_taxonomy( 'taxonomy-id', array( 'post', 'custom-post-type' ), $args );
}