1

In functions.php I can var_dump() taxonomies like:

$taxonomies=get_taxonomies('','names');
echo '<pre>';
var_dump($taxonomies);

which give me:

array(7) {
  ["category"]=>
  string(8) "category"
  ["post_tag"]=>
  string(8) "post_tag"
  ["nav_menu"]=>
  string(8) "nav_menu"
  ["link_category"]=>
  string(13) "link_category"
  ["post_format"]=>
  string(11) "post_format"
  ["project"]=>
  string(7) "project"
  ["team"]=>
  string(4) "team"
}

Now I would like to get all "categories" from my registered taxonomy "project"

I've tried with get_categories() and get_terms() both returning an empty array.

$taxonomies = array(
    'project'
);

$args = array(
    'orderby'           => 'name',
    'order'             => 'ASC',
    'hide_empty'        => true,
    'exclude'           => array(),
    'exclude_tree'      => array(),
    'include'           => array(),
    'number'            => '',
    'fields'            => 'all',
    'slug'              => '',
    'parent'            => '',
    'hierarchical'      => true,
    'child_of'          => 0,
    'get'               => '',
    'name__like'        => '',
    'description__like' => '',
    'pad_counts'        => false,
    'offset'            => '',
    'search'            => '',
    'cache_domain'      => 'core'
);

$terms = get_terms($taxonomies, $args);
var_dump($terms);

giving

array(0) {
}

What am I doing wrong?

  • Do you actually have posts in the terms under your custom taxonomy – Pieter Goosen Sep 18 '14 at 14:34
  • ooooooh dammmit @PieterGoosen : Now I have .. and it is working! what do you think, should I delete the question or would you like to write the comment as an answer? – caramba Sep 18 '14 at 14:38
  • 1
    There is one or two things I want to mention, so I will quickly do a proper answer :-) – Pieter Goosen Sep 18 '14 at 14:39
3

There are just a couple of things I want to point out here using get_terms

  • No need to put one value into an array, you can simply just add the value as is to the variable

  • To return your terms, you have to actually have posts assigned to them. By default, empty terms are hidden. The same goes for get_categories. If you need to show empty terms as well, just set the hide_empty parameter to false

  • You don't need to call the default values. If you are not going to change the value of a parameter, don't add it to your arguments. Just add those parameters that you are going to change

Here is a proper example

$args = array(
    'hide_empty' => false, 
);

$terms = get_terms('project', $args);
var_dump($terms);

Here we are getting all the terms, including empty ones, sorted by the default of names from the taxonomy project

  • Perhaps the best overview of get_terms() gotchas that I've seen to date. – staypuftman Oct 26 '16 at 12:40

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.