Paulund
2013-04-22 #wordpress

Creating A Custom Taxonomy In Wordpress

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 ); 
}

?>

  • $taxonomy - The name for the taxonomy.
  • $object_type - This can be a string or an array for the post types to assign the custom taxonomy.
  • $args - An array of arguments to define the custom taxonomy.

Arguments

The arguments that you can define for the taxonomy are: - label - The plural descriptive name for the taxonomy.

  • labels - An array of different descriptive names for the taxonomy.
  • public - A boolean value to if the taxonomy can be seen on the admin UI.
  • show_ui - A boolean value to generate the default UI in the admin area for the taxonomy.
  • show_in_nav_menus - A boolean value to decide if the taxonomy can be added to the navigation menus.
  • show_tagcloud - A boolean value to decide if the taxonomy can be used in a tag cloud.
  • show_admin_column - A boolean value to allow automatic creation of taxonomy columns on associated post-types.
  • hierarchical - A boolean value to decide if the taxonomy can be hierarchical.
  • update_count_callback - function name that will be called when the count of an associated $object_type.
  • query_var - A boolean value to decide if the taxonomy can be used in query_var.
  • rewrite - An array to define the rewrite rules for the taxonomy.
  • capabilities - An array to define the capabilities for the taxonomy.
  • sort - A boolean value to remember the order of the taxonomy.

Basic Example

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 );
}

UpChecker

Reliable uptime monitoring and instant alerts for any website downtime.

  • Uptime Monitoring
  • Performance Monitoring
  • SSL Certificate Alerts
  • Domain Monitoring
  • DNS Checker
  • XML Sitemap Monitoring