WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In edition to this post below:

wordpress json custom taxonomy problem

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

share|improve this question
up vote 2 down vote accepted

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

 * 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' ) {
            $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) {

  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

share|improve this answer
Thanks for the detailed answer :) Works like a charm! – B.S. Sep 15 '11 at 8:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.