WordPress json api taxonomy index method

In edition to this post below:

wordpress json custom taxonomy problem

Read More

How to write a method for getting categories withing a custom taxonomy, similar to Method: get_category_index in core json api controller?

Scenario:
I have a taxonomy called – books which has four categories and i need those categories to be outputted through get_taxonomy_index.

Something like this http://example.com//api/get_taxonomy_index/?dev=1

Related posts

Leave a Reply

1 comment

  1. To list terms of custom taxonomy in your case books we will need to create a custom controller for JSON API.

    Step 1: Following 2 classes should be paste in a php file stored in your theme directory (you can store the file whereever you like but then you will have to make sure you return the correct path of the file in code in Step 2). File name for this example is: json-api-taxonomy-index.php

    <?php
    /**
     * JSON API custom taxonomy index
     **/
    
    /**
     * Custom Taxonomy Controller for JSON API plugin
     *
     * This custom taxonomy controller enables json api to list all terms in specified taxonomy. 
     **/
    class JSON_API_Taxonomy_Controller {
    
        public function get_taxonomy_index() {
            $terms = $this->get_terms();
            return array(
                'count' => count( $terms ),
                'terms' => $terms
            );
        }
    
        public function get_terms() {
            global $json_api;
            $taxonomy = $this->get_current_taxonomy();
            if (!$taxonomy) {
                $json_api->error("Not found.");
            }
    
            $wp_terms = get_terms( $taxonomy );
            $terms = array();
            foreach ( $wp_terms as $wp_term ) {
                if ( $wp_term->term_id == 1 && $wp_term->slug == 'uncategorized' ) {
                    continue;
                }
                $terms[] = new JSON_API_Term( $wp_term );
            }
            return $terms;
        }
    
        protected function get_current_taxonomy() {
            global $json_api;
            $taxonomy  = $json_api->query->get('taxonomy');
            if ( $taxonomy ) {
                return $taxonomy;
            } else {
                $json_api->error("Include 'taxonomy' var in your request.");
            }
            return null;
        }
    }
    
    // Generic rewrite of JSON_API_Tag class to represent any term of any type of taxonomy in WP
    class JSON_API_Term {
    
      var $id;          // Integer
      var $slug;        // String
      var $title;       // String
      var $description; // String
    
      function JSON_API_Term($term = null) {
        if ($term) {
          $this->import_wp_object($term);
        }
      }
    
      function import_wp_object($term) {
        $this->id = (int) $term->term_id;
        $this->slug = $term->slug;
        $this->title = $term->name;
        $this->description = $term->description;
        $this->post_count = (int) $term->count;
      }
    
    }
    ?>
    

    Step 2: Paste the following filters in your functions.php file. If you haven’t stored the file in your theme directory then change the path of the file in set_taxonomy_controller_path function.

    function add_taxonomy_controller($controllers) {
      $controllers[] = 'Taxonomy';
      return $controllers;
    }
    add_filter('json_api_controllers', 'add_taxonomy_controller');
    
    function set_taxonomy_controller_path() {
      return get_stylesheet_directory() . '/json-api-taxonomy-index.php';
    }
    add_filter('json_api_taxonomy_controller_path', 'set_taxonomy_controller_path');
    

    Step 3: Goto Settings->JSON API and Activate taxonomy controller.

    Thats it you are done! Now you can access the terms of your custom taxonomy using the json api. Example: http://example.com/api/Taxonomy/get_taxonomy_index/?taxonomy=books