Get Categories for Custom Post Types and Taxonomies

I recently built my own basic downloads manager plugin because I wanted something stupid simple. The bare functionality I needed was the title, a description, the file, and categories — So, I created a custom post type and then added a few meta boxes with the Custom Metaboxes and Fields Class.

I wrote all of the above in less than 10 minutes. The problem  I then encountered was figuring out how to get the list of categories that a download was assigned to.

This is simple when just working with regular posts; Just use the get_categories() function. However, categories within custom post types are a different beast…

Custom Post Types DO NOT Have Categories

I spent hours trying to figure out how to get the categories from a custom post type. And you know what I figured out after all of that wasted time? That custom post types do not have categories…

You see, categories are a type of taxonomy that is built-in to WordPress and used to categorize posts. When we create a “category” for a custom post type, we are actually creating a completely new taxonomy (even though we may in fact call it ‘categories’).

Take this custom taxonomy example below:

function cw_cpp_slideshow_categories() {
	$field_args = array(
		'labels' => array(
		'name'              => 'Categories', 'taxonomy general name',
		'singular_name'     => 'Category', 'taxonomy singular name',
		'search_items'      => 'Search Categories',
		'all_items'         => 'All Categories',
		'parent_item'       => 'Parent Category',
		'parent_item_colon' => 'Parent Category:',
		'edit_item'         => 'Edit Category',
		'update_item'       => 'Update Category',
		'add_new_item'      => 'Add New Category',
		'new_item_name'     => 'New Category',
		'menu_name'         => 'Categories',
		'hierarchical' => true
	register_taxonomy( 'slideshow_categories', 'slideshow', $field_args );
add_action( 'init', 'cw_cpp_slideshow_categories', 0 );

This is some code that I use to add categories to a slideshow custom post type. Look specifically at the line where I call register_taxonomy() . Notice how I define the taxonomy as slideshow_categories , then assign it to post type slideshow , then I pass in various arguments, such as labels, in the 3rd parameter.

MindblownSo, while I created a taxonomy that looks and functions almost exactly like categories does with posts… I actually created a new taxonomy named slideshow_categories .

Confusing? Yea, a bit…

Well, here’s a little bit more confusion. You know what you usually call categories? Those are actually named terms in other taxonomies…

 So, How Do I Get Categories for Custom Post Types?

The answer is pretty simple — just use get_terms(). The only parameter you need to pass in to get_terms()  is a taxonomy name, slideshow_categories from the example above.

But, if you need to do some special ordering, excludes, etc… You can also pass an array of arguments as the second parameter. Check out the codex for get_terms().

Questions or Comments?

If this helped you or if you just find it funny how long it took me to figure this out… leave comment below!

19 thoughts on “Get Categories for Custom Post Types and Taxonomies”

  1. You have no idea how this just saved my sanity.
    It took me all freaking day trying to retrieve the categories of a custom post type. This is insane, and to me is just illogical that wp manages this in that way.

    I glanced at my database, specifically in wp_term_taxonomy table, and there it was.

    Anyway, thanks a lot !

  2. Hey man, sorry to bug ya. I am somewhat running into the same issue you had, but mine is a little bit different. I am able to pull in posts under a custom post type (in a list) and have those posts bucketed into the various categories that is shared with other posts (not post-types). I am now needing to take that a level deeper and now show the child categories in the buckets and have its posts listed under that category. If you have time maybe you could give some guidance on how I can achieve this. Here is the paste bin to that: Again only if you have time. Thanks in advanced.

  3. Thanks for simple understanding post 🙂

    just need some help. I have created custom post type and registered taxonomy. But on post edit page sidebar of custom post type, I have no option to choose or add category. Instead, I have sidebar to add tags there.

  4. Here’s what works:

    	function get_cpt_parent_cat_aka_taxonomy() {
    		global $post;
    		$parent_tax_name = 'Undefined parent taxonomy!';
    		$obj_taxonomies = get_object_taxonomies( $post->post_type, 'objects' );
    		$slugs = array();
    		if( !empty( $obj_taxonomies ) ) {
    			foreach ( $obj_taxonomies as $key => $tax ) {
    				if( $tax->show_ui === true && $tax->public === true && $tax->hierarchical !== false ) {
    					array_push( $slugs, $tax->name );
    			$terms = wp_get_post_terms( $post->ID, $slugs );
    			$term = get_term( $terms[ 0 ]->term_id, $terms[ 0 ]->taxonomy );
    			$parent_tax_name = $term->name;
    		return $parent_tax_name;
    1. Thanks for the example code Dameer. I went ahead and modified it slightly to allow for syntax highlighting.

      It looks like your code returns a taxonomy, which is equivalent to a grouping of terms. Once the taxonomy was found, I believe the user would still want to use get_terms() to return the terms, or what most people consider categories, for that taxonomy.

  5. Thanks for the awesome post – it all makes sense now!

    When I do get_terms(‘taxonomy’); I get an output of “array”.

    Where taxonomy is my CPT taxonomy name.

    Im looking into it now but any advice would be appreciated

Leave a Reply

Your email address will not be published. Required fields are marked *