Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress rest api v2 how to list taxonomy terms?

i am new to v2, i use v1 for long time, currently upgrade to v2, i try to get all the terms belong to specific custom taxonomy.

In v1 i can do this to get terms /taxonomies/location_category/terms

but in v2 i try /taxonomies/terms it return json error "code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status" :404}}

if just /taxonomies/location_category/ it didn't show anything terms belong to taxonomy.

i search the question on google for few hours didn't show any result, anyone can help please, thank you

like image 655
Xiao Xinqi Avatar asked Feb 25 '17 22:02

Xiao Xinqi


People also ask

How do I show taxonomy terms in WordPress?

Custom display of Terms in a WordPress Taxonomy$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all'); $terms = wp_get_post_terms( $post_id, $taxonomy, $args );

How do I find my current taxonomy name in WordPress?

get_the_terms( int|WP_Post $post, string $taxonomy ) Retrieves the terms of the taxonomy that are attached to the post.

How do I get data from API in WordPress?

If you want to use the Fetch API with WordPress, you simply have to call the fetch function in your JavaScript code. Follow that function with a . then handler to access the content. You can then display it on your website or in your web application.

Where are WordPress taxonomies stored?

Taxonomies and terms are stored in the following database tables: wp_terms – stores all of the terms. wp_term_taxonomy – places the term in a taxonomy. wp_term_relationships – relates the taxonomy to an object (for example, category to post)


1 Answers

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.

like image 161
Xiao Xinqi Avatar answered Dec 19 '22 12:12

Xiao Xinqi