Trying to list terms of a custom taxonomy using get_categories

I’m using this little snippet of code to try and list the terms of a custom taxonomy for use in a meta box select box but it doesn’t show anything when I implement it (Despite working with the default ‘category’ taxonomy)

$partners = array();
$partners_obj = get_categories(array('taxonomy' => 'partner-cat'));
$partners[''] = '-';
foreach ($partners_obj as $partner) {
    $partners[$partner->cat_ID] = $partner->cat_name;;
}

As you can see, my custom taxonomy is ‘partner-cat’ and there are posts in a custom post type in 2 separate terms of this taxonomy.

Any help would be most appreciated.

5 Answers
5

get_categories()

fetches taxonomies of type ‘categories’ particularly http://core.trac.wordpress.org/browser/tags/3.6/wp-includes/category.php#L0,
to fetch custom taxonomy you should use get_terms() instead, here http://codex.wordpress.org/Function_Reference/get_terms

$terms = get_terms( 'partner-cat', 'orderby=count&hide_empty=0' );
$count = count($terms);
if ( $count > 0 ){
 echo "<ul>";
 foreach ( $terms as $term ) {
   echo "<li>" . $term->name . "</li>";

 }
 echo "</ul>";
}

Make sure you specify the proper slug you registered for taxonomy and change WP_DEBUG to true in your config file to check for further errors, as you might be fetching the taxonomy before you are registering it and hence no results in that case you’d get an error.

can you paste your code for registering taxonomy?

Leave a Comment