Link to home
Start Free TrialLog in
Avatar of gplana
gplanaFlag for Spain

asked on

Why get_terms function is returning an empty array ?

Hi.

I created a custom taxonomy called "forum_category" for which I created some possible values.
I'm using this simple code to see the list of terms for this taxonomy:

     $terms = get_terms('forum_category');
     print_r($terms);

Open in new window


but print_r shows an empty array. Do you know why ? What should I do to get all the values from a taxonomy ?

Thank you.
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

What's the rest of the context of that code?  Where are you running it?
Avatar of gplana

ASKER

Thank you for your answer.

I'm running it on a php file which is called from a template. This taxonomy is attached to a bbPress topic, which is a kind of post called 'topic'.
But the full WordPress calls appear above it, right (e.g. wp_load(), etc)?
Without more code we're guessing a little bit here.  Does anything actually have that taxonomy associated with it, and if so, do any of those actually make use of any of those values?  It could be that nothing is returned because it might ignore unassociated terms.

If you know the post ID of a page which does make use of the taxonomy you can try using the_terms( ID, 'taxonomy') instead, to verify that things are appropriately associated.

If that's not helpful then the question would be:

are you trying to display all the terms in the taxonomy, as though it were a list of ALL the categories,
or are you trying to list all the values associated with a given post?

I've used the following code for taxonomy based tag clouds before:
<?php wp_cumulus_insert(array('args' => 'smallest=8&largest=20&number=100&taxonomy='.$term->taxonomy)); ?>

Open in new window

This should work equally well:
wp_tag_cloud( array('args' => 'smallest=8&largest=20&number=100&taxonomy=forum_category'));

Open in new window

Hopefully those help you figure out the root problem...
Avatar of gplana

ASKER

Jason 1178: all the rest of Wordpress functions works. In fact I get no error in executing get_terms() function, just I get an empty array instead of an array with the expected values.

dcowley: Thank you so much for your help. I can provide whatever part of code you need.What I'm trying to get is the possible values for a taxonomy (the list of forum categories in my case), not the values for this taxonomy on the actual post. In fact, I don't have actual post because this code is executing on a template that shows the list of posts (list of topics of a bbForum to be exact). I added this taxonomy to make user be able to select for which categories he wants to see the topics. Anyway, I will try the two functions you said and let you know, but get_terms is supposed to return an array with all the values for a taxonomy, isn't it ?
Avatar of gplana

ASKER

Hi again.

I tryied to add this function:
wp_tag_cloud( array('args' => 'smallest=8&largest=20&number=100&taxonomy=forum_category'));

Open in new window

but no results are shown.

It looks like this taxonomy is not found. Could the "_" sign on the name be a problem ?
ASKER CERTIFIED SOLUTION
Avatar of dcowley
dcowley
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gplana

ASKER

Wow ! It  works exactly as you said !!!

Thank you so much. I didn't know this "hide_empty" parameter. You save me a lot of hours of investigation.

Thank  you, thank you so much.
Nice work, dcowley!