admin管理员组

文章数量:1323323

I am tried to display all product category with bellow function .. getting a error Invalid taxonomy .. it's was working fine last 2 installation..

    function be_woocommerce_category_id(){
        $categories_array = array();
        $categories_array[0] = esc_html__('Choose a Category', 'shopstore'); 
        $args = array(
                    'orderby'    => 'title',
                    'order'      => 'ASC',
                );
        $categories = get_terms( 'product_cat', $args );

        if( count($categories) > 0 ){
            foreach( $categories as $category ){
              $categories_array[$category->term_id] = $category->name; 
            }
        }
        return $categories_array; 
}

I am tried to display all product category with bellow function .. getting a error Invalid taxonomy .. it's was working fine last 2 installation..

    function be_woocommerce_category_id(){
        $categories_array = array();
        $categories_array[0] = esc_html__('Choose a Category', 'shopstore'); 
        $args = array(
                    'orderby'    => 'title',
                    'order'      => 'ASC',
                );
        $categories = get_terms( 'product_cat', $args );

        if( count($categories) > 0 ){
            foreach( $categories as $category ){
              $categories_array[$category->term_id] = $category->name; 
            }
        }
        return $categories_array; 
}
Share Improve this question edited Feb 14, 2019 at 9:28 Tanmay Patel 8111 gold badge7 silver badges11 bronze badges asked Feb 14, 2019 at 7:31 Saiful IslamSaiful Islam 758 bronze badges 3
  • Where and when are you running this function? – Jacob Peattie Commented Feb 14, 2019 at 9:37
  • it's default woocomerce hook – Saiful Islam Commented Feb 14, 2019 at 9:41
  • Which hook? Specifically. – Jacob Peattie Commented Feb 14, 2019 at 9:48
Add a comment  | 

3 Answers 3

Reset to default 1

it's was working fine last 2 installation

Those two installation might still be on WordPress prior to version 4.5.0, and according to the reference:

Since 4.5.0, taxonomies should be passed via the ‘taxonomy’ argument in the $args array

So:

$args = array(
  'orderby'  => 'title',
  'order'    => 'ASC',
  'taxonomy' => 'product_cat',
);
$categories = get_terms( $args );

If that doesn't work, then maybe on the new installation, your code is executed before the product_cat taxonomy is registered.

I had the same 'problem' a few days ago. Like Sally CJ edited, this may be a registration issue.
When I called term_exists(), the expected term_id was returned, but get_term() returned the Invalid taxonomy error.
My solution was to call the register_taxonomy function in my function and everything was fine.
Edit:
I called it like that (with an empty array as $args):

register_taxonomy( 'taxonomy_name', array('the_custom_post_type'), array() );

I had the same problem. For Woocomerce you can be resolved by adding the below code in functions.php:

register_taxonomy( 'product_cat', array('product'), array() );

本文标签: Can39t display product categories on woocommerce getting Invalid taxonomy