1

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.

1

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>';
  • No, they both have posts in them. get_terms( 'partner-cat', array('hide_empty' => false) ); didn't work either... It's odd because it works fine with the default post categories – Poisontonomes Aug 14 '13 at 10:39
  • Updated answer. it works for me. – RRikesh Aug 14 '13 at 10:43
  • I gave that a try and still no luck, although it did throw up an error telling me the taxonomy was invalid.... I've double and triple checked the name and it's correct so I don't know why it's doing that – Poisontonomes Aug 14 '13 at 10:52
  • get_terms return wP_Error when the taxonomy does not exist. Could you try with another taxonomy? how are you creating this one? codes or plugin? – RRikesh Aug 14 '13 at 10:55
  • I've just tried it with another custom taxonomy, same error. Then I tried it with the default "category" one and it worked. Both custom taxonomies do exist though and both have posts... I'm coding it in myself, no plugin – Poisontonomes Aug 14 '13 at 10:58
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?

  • I didn't even think of that! I was including the meta box function BEFORE my custom taxonomy function. I switched them around and it works perfectly now. Thank you! – Poisontonomes Aug 14 '13 at 11:01
  • ok great... :), easy solution, so you should mark it as answer I guess :P – Kumar Aug 14 '13 at 11:02
0

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; 
?>
  • Just a tip, rather use variables like $terms and $term when working with term related functions like get_terms. It is less confusing than using a variable like $categories :-) – Pieter Goosen Mar 31 '15 at 10:25
  • @PieterGoosen I know :), that's why I changed my variables from $terms to $categories :) – Mile Milosheski Apr 1 '15 at 13:08
0

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_includes\category.php

0
            <?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;
                ?>
  • 1
    While sharing the source code to answer the question is helpful, it's also helpful to give an explanation. May you please answer how this code helps? – Ethan Jinks O'Sullivan Aug 24 '16 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.