How can I list the subcategories on woocommerce?

This is quite “simple” question, still, I havent found a good way to solve this.

What I’m trying to do is, before apply a filter, I want to get an list of the subcategories (I need their ID’s) .

Read More

So, I tried with this

$args = array( 'hierarchical' => 1, 
    'show_option_none' => '', 
    'hide_empty' => 0, 
    'parent' => 12, 
    'taxonomy' => 'product_cat' ); 
$subcats = get_categories($args);

But seems like something is not good and I get this:

Array ( [WP_Errorerrors] => Array ( [invalid_taxonomy] => Array ( [0] => Invalid taxonomy ) ) [WP_Errorerror_data] => Array ( ) ) 

If I move the code where my filter is, I get an infinite loop error (which is worse), Still, I havent found why it is an invalid taxonomy… is there any easier way to get this?

Thanks.

Related posts

Leave a Reply

1 comment

  1. get_categories() automatically assumes the taxonomy is category. Product Category is a custom taxonomy (product_cat), so you need to use get_terms()

    function sub_cats(){
        $args = array( 'hierarchical' => 1, 
            'hide_empty' => 0, 
            'parent' => 12 ); 
        $subcats = get_terms( 'product_cat', $args);
    
    var_dump($subcats); 
    }
    add_action('init', 'sub_cats', 20 );