0

I have custom taxonomies

like that

/wp-admin/edit-tags.php?taxonomy=my_cat&post_type=my_poet

I want retrieve list of taxonomies in json

add_action('init', array($this, 'json_handler'));

I tried

function json_handler(){
$categories = get_terms( 'my_cat', 'orderby=count&hide_empty=0' );
print_r($categories);
}

Outpot

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

But it working for normal category

$categories = get_terms( 'category', 'orderby=count&hide_empty=0' );
    print_r($categories);

Outpot

Array ( [0] => stdClass Object ( [term_id] => 1 [name] => Uncategorized [slug] => uncategorized [term_group] => 0 [term_taxonomy_id] => 1 [taxonomy] => category [description] => [parent] => 0 [count] => 1 ) )

Here is my_cat register_taxonomy

add_action('init', array($this, 'create_stores_nonhierarchical_taxonomy'));
function create_stores_nonhierarchical_taxonomy() {
// Labels part for the GUI
$labels = array(
'name' => _x( 'Store  Categories', $this -> textdomain ),
'singular_name' => _x( 'Store  Categories', $this -> textdomain ),
'search_items' =>  __( 'Search Store  Categories',$this -> textdomain ),
'popular_items' => __( 'Popular Store  Categories',$this -> textdomain ),
'all_items' => __( 'All Store  Categories ',$this -> textdomain ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Store Categories',$this -> textdomain ), 
'update_item' => __( 'Update Store Categories' ,$this -> textdomain),
'add_new_item' => __( 'Add New Store Categories',$this -> textdomain ),
'new_item_name' => __( 'New Store Categories Name',$this -> textdomain ),
'add_or_remove_items' => __( 'Add or remove Store Categories',$this -> textdomain ),
'choose_from_most_used' => __( 'Choose from the most used Store Categories',$this -> textdomain ),
'menu_name' => __( 'Store Categories',$this -> textdomain ),
);
// Now register the non-hierarchical taxonomy like tag
register_taxonomy('my_cat',$this->post_type,array(
'labels'            => $labels,
    'hierarchical'      => true,
    'show_ui'           => true,
    'how_in_nav_menus'  => true,
    'public'            => true,
    'show_admin_column' => true,
    'query_var'         => true,
'rewrite' => array( 'slug' => 'my_cat' ),
));
}

Thanks

1

Your problem is that your trying to access taxonomy data before it is registered.

This doesn't work:

add_action('init', 'json_handler');
function json_handler(){
    $categories = get_terms( 'my_cat', 'orderby=count&hide_empty=0' );
    if( ! is_wp_error( $categories ) ) {
        // encode the $categories array as json
        print_r( json_encode( $categories ) );
    }
}

add_action('init', 'create_stores_nonhierarchical_taxonomy');
function create_stores_nonhierarchical_taxonomy() {
    // Labels part for the GUI
    $labels = array(
        'name' => _x( 'Store  Categories', $this -> textdomain ),
        //Rest of your labels
    );
    // Now register the non-hierarchical taxonomy like tag
    register_taxonomy('my_cat',$this->post_type,array(
        'labels'            => $labels,
        'hierarchical'      => true,
        'show_ui'           => true,
        'how_in_nav_menus'  => true,
        'public'            => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'my_cat' ),
    ));
}

This works:

add_action('init', 'create_stores_nonhierarchical_taxonomy');
function create_stores_nonhierarchical_taxonomy() {
    // Labels part for the GUI
    $labels = array(
        'name' => _x( 'Store  Categories', $this -> textdomain ),
        //Rest of your labels
    );
    // Now register the non-hierarchical taxonomy like tag
    register_taxonomy('my_cat',$this->post_type,array(
        'labels'            => $labels,
        'hierarchical'      => true,
        'show_ui'           => true,
        'how_in_nav_menus'  => true,
        'public'            => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'my_cat' ),
    ));
}

add_action('init', 'json_handler');
function json_handler(){
    $categories = get_terms( 'my_cat', 'orderby=count&hide_empty=0' );
    if( ! is_wp_error( $categories ) ) {
        // encode the $categories array as json
        print_r( json_encode( $categories ) );
    }
}

Change the order, as I did above, is not a good solution for maintainability. A better solution would be:

1.- Register the taxonomy on init with low priority (default priority is 10, actions with same priority are executed in the order they appear):

add_action( 'init', array($this, 'create_stores_nonhierarchical_taxonomy'), 1 );

2.- Try to access taxonomy data after init event, so you are sure the taxonomy has been registered, or on init events with priority higher than priority of taxonomy register callback, in this example with priority higher than 1.

Note: registering a taxonomy before or after init is not recommended.

  • thanks for your answer . I apply registering a taxonomy in separate file and json_handler in another file – abada henno May 26 '15 at 15:18
  • In API.php add_action('init', array($this, 'json_handler'),999); in stores.php add_action('init', array($this, 'create_stores_nonhierarchical_taxonomy'),1); Doesn`t work :( – abada henno May 26 '15 at 15:25
  • The tick is show custom taxonomies in json format by direct link form my plugin like this ?endpoint=my_cat – abada henno May 26 '15 at 15:26
  • The code I posted has been tested and the problem you described in the question was solved (the WP_Error message). What other problem do you have? Is still that error message appearing? If the problem now is another one, please describe it, maybe better in a separate question to stick with one question per thread. – cybmeta May 26 '15 at 16:05
  • yes you are right your answer working but no in my case , i will write another question – abada henno May 26 '15 at 16:14

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.