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.

Read More

Any help would be most appreciated.

Related posts

5 comments

  1. 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?

  2. Are your taxonomy terms empty? They will not show by default. You could try using:

    $partners_obj = get_terms( 'partner-cat', array('hide_empty' => false) );
    

    Note that get_categories() uses get_term() to fetch your terms.

    Your foreach isn’t good too. you should access term_id and name instead.

    foreach ($partners_obj as $partner) {
        $partners[$partner->term_id] = $partner->name;
    }
    

    Whole code:

    $partners = array();
    
    $partners_obj =  get_terms( 'taxo-name-here', array('hide_empty' => false) );
    echo '<pre>' . print_r( $partners_obj, true ) . '</pre>';
    echo '<hr>';
    $partners[''] = '-';
    foreach ($partners_obj as $partner) {
        $partners[$partner->term_id] = $partner->name;;
    }
    
    echo '<pre>' . print_r( $partners, true ) . '</pre>';
    
  3. if you are using Custom Post Types CPT and I assume you’ve created custom taxonomy for that custom post type lets say:

    You have CPType named Products and you have CPTaxonomy named Products Categories

    If you are trying to display the CPTaxonomy into your products page somewhere in the loop you I would suggest to use this function according WP codex:

    http://codex.wordpress.org/Function_Reference/get_terms

    the code to get the CPTaxonomy for the CPType both named products should be the following:

    $categories = get_terms( 'products', 'orderby=count&hide_empty=0' );
    

    then for displaying the taxonomies use this code:

    <?php foreach($categories as $category):
     print $category->slug;print $category->name; 
    ?>
    
  4. The WordPress way:

    $categories = (array) get_terms('partner-cat', array('hide_empty' => false));
    foreach (array_keys($categories) as $k)
    {
        _make_cat_compat($categories[$k]);
    }
    

    This is using the direct implementation that WordPress uses in function get_categories() located within wp_includescategory.php

  5.             <?php
                    $args = array(
                        'type' =>'',//optional
                        'number' => '',
                        'hide_empty' => 0,
                        'taxonomy'  => 'product_cat' //your custom taxonomy name
                    ); 
                    $categories = get_categories($args); 
                    //echo "<pre>";   print_r($categories);
                    foreach($categories as $cat){
                        $catid = $cat->term_id;                 
                        $thumbnail_id = get_woocommerce_term_meta($catid, 'thumbnail_id', true );
                        $image = wp_get_attachment_url( $thumbnail_id );
                        $catname = $cat->name;
                        $catpage = $cat->slug;
                    ?>
    

Comments are closed.