Using a custom taxonomy is a really easy way to separate posts within a post type. Custom taxonomies have numerous benefits, added flexibility, and are easy to implement.
Creating a custom taxonomy provides an easy way to separate posts without cluttering up the existing “category” or “tag” taxonomies. Designed specially for use with WordPress hooks, these taxonomies should be reserved for “posts” . Custom taxonomies can be attached to either the sites posts, or a custom post type and can also be used across multiple post types without causing conflicts.Custom taxonomies should be added via a plugin to allow continued functionality on a theme change, but can also just be added in the themes functions.php file. The WordPress codex also contains helpful and in-depth information on custom taxonomies.
Adding a Custom Taxonomy
The php for a custom taxonomy is fairly straightforward. It’s mostly making sure that the names and labels are correct. Make sure the hook is called in a function on 'init' . This will ensure your taxonomy always exists when you want to use it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
function products_post_type() { $labels = array( 'name' => 'Taxonomy', 'singular_name' => 'Taxonomy', 'search_items' => 'Search Taxonomies', 'all_items' => 'All Taxonomies', 'parent_item' => 'Parent Taxonomy', 'parent_item_colon' => 'Parent Taxonomy:', 'edit_item' => 'Edit Taxonomy', 'update_item' => 'Update Taxonomy', 'add_new_item' => 'Add New Taxonomy', 'new_item_name' => 'New Taxonomy', ); $args = array( 'hierarchical' => true, 'rewrite' => array('slug' => 'taxonomy', 'with_front' => false), 'show_in_nav_menus' => true, 'labels' => $labels, 'show-admin-column' => true, 'show_in_nav_menus' => true ); register_taxonomy('taxonomy', 'post-type', $args); unset($labels); unset($args); } add_action('init', 'products_post_type'); |
The most important part of this code is the line 'hierarchical' => true, . This determines whether the taxonomy is grouped and will contain subcategories, or is just a list of terms like tags. To achieve tag-like functionality, use 'hierarchical' => false, instead.
Terms in the 'rewrite' => array(), are also important to the taxonomies functionality. This array determines determines the url for the archives of the custom taxonomy. In this example, the posts in the new taxonomy are found at your-url.com/taxonomy/post-taxonomy/post-title. If you have a hierarchical taxonomy and wish to include all the sub-categories in the url, add 'hierarchical' => true, into the array.
Don’t forget to change the “post-type” to the post type that you want to attach the taxonomy to. To add the custom taxonomy to a standard blog post, use register_taxonomy('taxonomy', 'post', $args); . This simple WordPress function allows much greater flexibility in categorising posts. Custom post types are in action all over my site, and are most noticeable in the portfolio. Those categories? All part of a custom taxonomy attached to the custom post type.
Creating a Taxonomy within a Custom post Type
If linking taxonomies and custom post types is giving you grief, it may be easier to create the taxonomy at the same time as the post type. The following code shows both a custom post type and a custom taxonomy. It also builds on the custom post type tutorial found here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
function products_post_type() { $labels = array( 'name' => 'Products', 'singular_name' => 'Products', 'add_new' => 'Add New', 'add_new_item' => 'Add New Product', 'edit_item' => 'Edit Product', 'new_item' => 'New Product', 'all_items' => 'All Products', 'view_item' => 'View Product', 'search_items' => 'Search Products', 'not_found' => 'No products found', 'not_found_in_trash' => 'No products found in Trash', 'parent_item_colon' => '', 'menu_name' => 'Products' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'taxonomies' => array('productcategory', 'surfaces'), 'rewrite' => array('hierarchical' => true, 'slug' => 'products', 'with_front' => false), //Adding custom rewrite tag 'capability_type' => 'post', 'has_archive' => 'productcategory', 'hierarchical' => true, 'menu_position' => null, 'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'), 'register_meta_box_cb' => 'products_custom_meta', ); register_post_type('products', $args); unset($labels); unset($args); $labels = array( 'name' => 'Product Categories', 'singular_name' => 'Product Categories', 'search_items' => 'Search Product Categories', 'all_items' => 'All Product Categories', 'parent_item' => 'Parent Product Category', 'parent_item_colon' => 'Parent Product Category:', 'edit_item' => 'Edit Product Category', 'update_item' => 'Update Product Category', 'add_new_item' => 'Add New Product Category', 'new_item_name' => 'New Product Category', ); $args = array( 'hierarchical' => true, 'rewrite' => array('hierarchical' => true, 'slug' => 'products', 'with_front' => false), 'show_in_nav_menus' => true, 'labels' => $labels, 'show-admin-column' => true, ); register_taxonomy('productcategory', 'products', $args); unset($labels); unset($args); } add_action('init', 'products_post_type'); |
This code is combining the custom taxonomy and the custom post type into one single function. They are linked to each other, and the post permalink (with the category slug “products”) containing the full heirachy. The largest difference between this function and original post type function is the name of the archive category. Be sure to set the archive to the new one, otherwise WordPress will use the default categories.
Creating a linking taxonomies is easy. To avoid unwanted interaction with other plugins, be sure to include an identifier with your function and the id. This also helps to identify your post types when called on other pages.