Setting up custom taxonomies in WordPress

Custom taxonomies in WordPress are, amongst other things, suited to a grouping classes of things, much like a tag might be used. The difference between tags and taxonomies is that taxonomies can be grouped in a hierachical manner, with parents and children. On the Clients page on creativeglo.co.uk there is a list of clients to which I wanted to add lists of work undertaken for each client. I did this by creating a custom taxonomy for clients. Set out below is an overview setting up the custom taxonomy.

Setting up custom taxonomies requires some code in the functions.php file. First a function inside which each individual taxonomy will be registered, and an add_action() to let WordPress know they are there.

function my_taxonomies() {
// taxonomies go here
}
add_action( 'init', 'my_taxonomies', 0 );

As others have taken the time to describe in detail the options available, I’ll not do so here. See the links at the foot of the article for links to resources. It’s worth noting here that I’m setting up a custom taxonomy called client_list which is being made available, via the array¬†array(‘my_clients’,'post’), to both regular posts as well as to custom posts of the type my_clients.

function myCustomTaxonomies() {
register_taxonomy( 'client_list',
array('my_clients','post'),
array(
'hierarchical' => false,
'label' => 'Client List',
'query_var' => true,
'show_ui' => true,
'rewrite' => true,
)
);
}
add_action( 'init', 'my_taxonomies', 0 );

The UI for the taxonomies should now be visible in the admin menu sidebar menu.

Custom Taxonomy in admin menu

Next I could have created a taxonomy to match each client and manually added the appropriate taxonmy to each project. However, as each project post had an existing custom field which matched projects to clients I looked to see if there was some way to avoid all that manual work. It turned out that there was. There will be more on this  in the next post. With the taxonomies were set up, the admin screen looks like as below, with fields for creating a new taxonomy on the left, and a list of existing taxonomies on the right.

Admin screen showing custom taxonomies

Because client_list was made available to both regular posts and custom posts, it should now be visible on both of those screens. Regular post screen:

Custom taxonomy panel on a regular post admin screen

Custom post screen:

Custom taxonomy panel on a custom post admin page

Once both clients and projects have been assigned the appropriate taxonomy it’s possible reference the taxonomy, ¬†and display posts by querying it. The code below displays the taxonomy for an individual post. Note that as¬†client_list is a group of taxonomies, ¬†$client list returns an array. If I accudently associated a project with more than one client, $client would simply return ‘array’. So this code only works as intended when the a array contains only one item.


<?php	// client as custom taxonomy
$client = get_the_term_list( $thePostID, 'client_list', '', '', '' ) ;
$client = strip_tags($client);
if ($client !== '') {
echo $client;
}
?>

Which displays on a page like this:

Custom taxonomy being displayed

The following code will retrieve projects by taxonomy, where client_list is the parent taxonomy, $client should be replaced with the taxonomy term you wish to search by:


<?php	// loop through all posts with the custom taxonomy in the 'client_list' which matches $client
$workposts = get_posts(array('client_list'=> $client, 'orderby'=>title, 'order'=>ASC, 'posts_per_page' => -1));
?>

<?php foreach ($workposts as $post) : ?>

<!-- looping code here-->

<?php	endforeach; // end client projects loop ?>

Now both the custom post type and taxonomies are set up, it's time to pull it all together.

Tags: