-1

I created a custom taxonomy through the code below:

add_action('init', 'vendor_taxonomy', 0);

function vendor_taxonomy(){

    $labels = array(
        'name'                       => 'Vendors',
        'singular_name'              => 'Vendor',
        'menu_name'                  => 'Vendors',
        'all_items'                  => 'All Vendors',
        'parent_item'                => 'Parent Vendor',
        'parent_item_colon'          => 'Parent Vendor:',
        'new_item_name'              => 'New Vendor Name',
        'add_new_item'               => 'Add New Vendor',
        'edit_item'                  => 'Edit Vendor',
        'update_item'                => 'Update Vendor',
        'separate_items_with_commas' => 'Separate Vendors with commas',
        'search_items'               => 'Search Vendors',
        'add_or_remove_items'        => 'Add or remove Vendors',
        'choose_from_most_used'      => 'Choose from the most used Vendors',
    );


    $capabilities = array(
        'manage_terms' => 'manage_woocommerce',
        'edit_terms' => 'manage_woocommerce',
        'delete_terms' => 'manage_woocommerce',
        'assign_terms' => 'manage_woocommerce',
    );

    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => false,
        'public'                     => true,
        'show_ui'                    => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => true,
        'show_in_rest'               => true,
        'query_var'                  => true,
        'rest_base'                  => 'vendors',
        'rest_controller_class'      => 'WP_REST_Terms_Controller',
        'capabilities'               => $capabilities,
        'rewrite'                    => array('slug' => 'vendors'),


    );


    register_taxonomy('vendors', 'product', $args);
    register_taxonomy_for_object_type('vendors', 'product');
}

I want to access the taxonomy list from Woocommerce rest API, but I always get 404.

I've tried the following endpoints:

  • /wp-json/wc/v3/products/vendors/
  • /wp-json/wc/v3/vendors/

Which should be the correct url?
Am I missing something in the taxonomy creation in order to retrieve data via API?

Ruvee
  • 8,611
  • 4
  • 18
  • 44

1 Answers1

1

You could access it through the following endpoint:

/wp-json/wp/v2/{your custom taxonomy}

So for your "vendors" custom taxonomy, it would be something like this:

/wp-json/wp/v2/vendors

This will retrieve the related data for each vendor.

For more details:

How to retrieve custom taxonomy on wordpressDocs


If you need to retrieve the data related to the "your custom taxonomy" itself, then you could use the following endpoint:

/wp-json/wp/v2/taxonomies/{your custom taxonomy}

For example, for your "vendors" custom taxonomy, it would be something like this:

/wp-json/wp/v2/taxonomies/vendors

For more details:

How to retrieve the related data on custom taxonomy itselfDocs


As a POC:

I have two vendors called "test vendor 01" and "test vendor 02", and I can retrieve them using:

/wp-json/wp/v2/vendors

And here's the results:

enter image description here

enter image description here

Ruvee
  • 8,611
  • 4
  • 18
  • 44
  • Thanks for your suggestions. Indeed the taxonomy is available in Wordpress API and under /wp-json/wp/v2/vendors. I can use this, but originally I was looking to retrieve it from Woocommerce API. In my use case "vendors" is a taxonomy of "products" in Woocommerce. Since I'm building an environment that interact mainly with Woocommerce (product, orders, etc...) it would have been ideal for me to work only with one API instead of two. – Maurizio Bellini Jan 02 '22 at 20:01
  • It was NOT a suggestion, it was the answer to your question. You used `register_taxonomy` function to register your taxonomy right? You've registered your taxonomy with wordpress. Woocommerce is a plugin on top of the wordpress. "product", "order" etc. are custom post types registered in wordpress. Wordpress and woocommerce are **NOT** separate. Interacting "mainly with woocommerce" is interacting with wordpress. `/wp-json/wp/v2/` exists whether you use it or not, and that's your answer whether you use it or not. – Ruvee Jan 02 '22 at 20:35