Get_terms gives an "invalid taxonomy" from the plugin

I am creating a plugin for woocommerce and I have some problems. I am trying to get all available product categories.

the code looks something like this:

$cats = get_terms('product_cat', array('hide_empty' => 0, 'orderby' => 'ASC', 'parent' =>0)); print_r($cats); 

It gives me

 WP_Error Object ( [errors:WP_Error:private] => Array ( [invalid_taxonomy] => Array ( [0] => Invalid taxonomy ) ) [error_data:WP_Error:private] => Array ( ) ) 

Does it need to be connected to some special init or something else? I tried the same code in functions.php, but with the same error.

EDIT: Yes, I found soluiton to the problem. I added

 add_action('init', 'runMyPlugin'); 

did the trick!

+6
source share
1 answer

Just add a complete code example.

 add_action('init', 'my_get_woo_cats'); function my_get_woo_cats() { $cats = get_terms( array( 'taxonomy' => 'product_cat','hide_empty' => 0, 'orderby' => 'ASC', 'parent' =>0) ); print_r($cats); } 
0
source

Source: https://habr.com/ru/post/977219/


All Articles