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:
Which should be the correct url
?
Am I missing something in the taxonomy creation in order to retrieve data via API?
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:
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 itself
Docs
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: