wordpress rest api v2 how to list taxonomy terms?
end out to write the custom code here
add blow code to functions.php
class all_terms{ public function __construct() { $version = '2'; $namespace = 'wp/v' . $version; $base = 'all-terms'; register_rest_route($namespace, '/' . $base, array( 'methods' => 'GET', 'callback' => array($this, 'get_all_terms'), )); } public function get_all_terms($object) { $return = array(); // $return['categories'] = get_terms('category'); // $return['tags'] = get_terms('post_tag'); // Get taxonomies $args = array( 'public' => true, '_builtin' => false ); $output = 'names'; // or objects $operator = 'and'; // 'and' or 'or' $taxonomies = get_taxonomies($args, $output, $operator); foreach ($taxonomies as $key => $taxonomy_name) { if($taxonomy_name = $_GET['term']){ $return = get_terms($taxonomy_name); } } return new WP_REST_Response($return, 200); }}add_action('rest_api_init', function () { $all_terms = new all_terms;});
and enter url http://youdomain.com/wp-json/wp/v2/all-terms?term=you_taxonomy
so term = you_taxonomy, will get terms belong to job_category.
For custom taxonomies, be sure that you're setting the 'show_in_rest' argument to be true (default is false) in your register_taxonomy()
call.
The register_taxonomy()
call is also where you can set the 'rest_base' argument (default will be the taxonomy name, /location_category/
in your example).
Taxonomy terms are simply called this way:
https://yoursite.com/wp-json/wp/v2/the-taxonomy-slug
For instance, to answer your question:
https://yoursite.com/wp-json/wp/v2/location_category
From terminal:
curl -X GET -i http://www.example.com/wp-json/wp/v2/location_category