get_terms won’t display product_cat or any other custom taxonomies when specified

Wondering if you might have some ideas on this problem. I’m googled for days but can’t figure it out.

Here’s where I’m at:

I have a meta box for the Woocommerce post type ‘products’. Inside the meta box there’s a 'type' = > 'select' that I want to populate with a list of all of the available 'taxonomy' = > 'product_cat'.

I can get the select box to populate and work with the standard post categories, 'taxonomy' = > 'category' using the following code:

function product_cats() {
$options = array();

$categories = get_terms( array( 'taxonomy' => 'category' ) );
foreach( $categories as $category ) {
    $options[$category->term_id] = array(
        'label' => $category->name,
        'value' => $category->slug
    );
}
// return array('options'=>$options);
return $options;
}

It all falls apart though when I try to use ‘taxonomy' = > ‘product_cat’ or any other custom taxonomy I have.

I thought the issue was that I’m trying to access the custom taxonomy before it’s being registered, so I swapped around some declarations/calls in my function.php file (ones which call the CPT, meta boxes, and woocommece) to potentially change the order that things run in but no luck.

BUT, based on the question and answer below, I can now confirm that the function can ‘see’ and display all terms, across taxonomies. If I exclude the 'taxonomy => from the arguments it returns terms from across custom post types and taxonomies.

Ideally the basic function would read:

function product_cats() {
$options = array();

$categories = get_terms( array( 'taxonomy' => 'product_cat' ) );
foreach( $categories as $category ) {
    $options[$category->term_id] = array(
        'label' => $category->name,
        'value' => $category->slug
    );
}
// return array('options'=>$options);
return $options;
}

Just wondering if you had any general thoughts? I know it’s difficult without seeing the whole code base, but I thought it would be worth an ask.

WordPress Version 4.7.2

Woocommerce Version 2.6.14

UPDATE:

Slowly I’m trying to pinpoint my issue.

It appears that 'product_cat' can be accessed after all (good) but it’s spitting out an array that’s not displaying properly.

This is confusing to me as if I simply use get_terms() without any parameters, or specifying 'taxonomy' => 'category' the code above works flawlessly

The other pieces of code that I need to work with this are:

The array that I’d like the list of options to dump in to

    array(  
        'label'=> 'Collections',
        'desc'  => 'Select the collection you would like to display',
        'id'    => $prefix.'collection',
        'type'    => 'select',  
        'options' => product_cats()
),

the code which generates the select list (used for other meta fields)

// select
case 'select':
echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';
foreach ($field['options'] as $option) {
    echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'">'.$option['label'].'</option>';
}
echo '</select><br /><span class="description">'.$field['desc'].'</span>';
break;

I have zero issues with any other meta fields working or displaying, including select lists.

I’d rather no re-write the entire meta box with all of its fields so I’m trying to work with what I have at the moment.

4 Answers
4

You are maybe running an old version of WordPress (before 4.5).

Before WordPress 4.5.0, the first parameter of get_terms() was a taxonomy or list of taxonomies and since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array (that what you are doing, it should work like that).

You will find all the details about these changes in the get_terms() reference page.

UPDATE :
Sorry, I verify my code, and I use get_categories() not get_terms, and that right get_terms() don’t work !

here is a working example to list all my product_cat

$product_categories = get_categories( array(
    'taxonomy'     => 'product_cat',
    'orderby'      => 'name',
    'pad_counts'   => false,
    'hierarchical' => 1,
    'hide_empty'   => false
) );

Hope it helps !

Leave a Comment