I created 1 custom post type with 2 custom taxonomy. Installed http://wordpress.org/extend/plugins/json-api/ and tried to reach result with: http://example.com/api/get_recent_posts?dev=1&post_type=myposttype

well it gives me custom posts but not giving those post's custom taxonomies

"categories": [], "tags": [],

How can i query custom taxonomies with json api?

Actually i am trying to create a simple iphone app with jquery mobile + phonegap. Maybe you know a better way then json api?

3 Answers 3


I would have done it as following, I am sure experts here will have a better way but following is what I could come up with in hurry.

First create your controller file in your theme directory (or any other if you like) with the following content. For this example the file name is korkmaz.php

UPDATE 1: Please replace the previous korkmaz.php because extending introspector was dangerous many unwanted functions were exposed via URI. Now we have a new JSON_API_Korkmaz_Controller class which does not extend any other class and We have removed the JSON_API_CustomPost class.

UPDATE 2: Now supports querying custom taxonomies, see the example at the bottom. Includes new model class JSON_API_Term to represent term of any type of taxonomy.

// most of the functions here are rewrite of json-api functions
class JSON_API_Korkmaz_Controller {

    public function get_recent_posts() {
        global $json_api;

        $posts = $json_api->introspector->get_posts();
        foreach ($posts as $jpost) {
            $this->add_taxonomies( $jpost );
        return $this->posts_result($posts);

    protected function posts_result($posts) {
        global $wp_query;
        return array(
            'count' => count($posts),
            'count_total' => (int) $wp_query->found_posts,
            'pages' => $wp_query->max_num_pages,
            'posts' => $posts

    protected function add_taxonomies( $post ) {
        $taxonomies = get_object_taxonomies( $post->type );
        foreach ($taxonomies as $tax) {
            $post->$tax = array();
            $terms = wp_get_object_terms( $post->id, $tax );
            foreach ( $terms as $term ) {
                $post->{$tax}[] = $term->name;
        return true;

    public function get_taxonomy_posts() {
        global $json_api;
        $taxonomy = $this->get_current_taxonomy();
        if (!$taxonomy) {
            $json_api->error("Not found.");
        $term = $this->get_current_term( $taxonomy );
        $posts = $json_api->introspector->get_posts(array(
                    'taxonomy' => $taxonomy,
                    'term' => $term->slug
        foreach ($posts as $jpost) {
            $this->add_taxonomies( $jpost );
        return $this->posts_object_result($posts, $taxonomy, $term);

    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;

    protected function get_current_term( $taxonomy=null ) {
        global $json_api;
        extract($json_api->query->get(array('id', 'slug', 'term_id', 'term_slug')));
        if ($id || $term_id) {
            if (!$id) {
                $id = $term_id;
            return $this->get_term_by_id($id, $taxonomy);
        } else if ($slug || $term_slug) {
            if (!$slug) {
                $slug = $term_slug;
            return $this->get_term_by_slug($slug, $taxonomy);
        } else {
            $json_api->error("Include 'id' or 'slug' var for specifying term in your request.");
        return null;

    protected function get_term_by_id($term_id, $taxonomy) {
        $term = get_term_by('id', $term_id, $taxonomy);
        if ( !$term ) return null;
        return new JSON_API_Term( $term );

    protected function get_term_by_slug($term_slug, $taxonomy) {
        $term = get_term_by('slug', $term_slug, $taxonomy);
        if ( !$term ) return null;
        return new JSON_API_Term( $term );

    protected function posts_object_result($posts, $taxonomy, $term) {
        global $wp_query;
        return array(
          'count' => count($posts),
          'pages' => (int) $wp_query->max_num_pages,
          'taxonomy' => $taxonomy,
          'term' => $term,
          'posts' => $posts


// 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;


Now add following to your theme's functions.php

// Add a custom controller
add_filter('json_api_controllers', 'add_my_controller');
function add_my_controller($controllers) {
  $controllers[] = 'Korkmaz';
  return $controllers;

// Register the source file for our controller
add_filter('json_api_korkmaz_controller_path', 'korkmaz_controller_path');
function korkmaz_controller_path($default_path) {
  return get_stylesheet_directory() . '/korkmaz.php';

Now goto Json API settings page and enable your korkmaz controller.

Now you can access your custom post types with all of the associated taxonomies at the following url: http://example.com/api/korkmaz/get_recent_posts/?post_type=myposttype

You can query any type of taxonomy (including custom taxonomies) using the following example: http://example.com/api/korkmaz/get_taxonomy_posts/?taxonomy=my_custom_taxonomy&slug=my_term_slug

  • Great job! This works really well. If anyone is interested on how to add "posts_per_page" or "offset" or anything else, you just need to add it to the array that's sent with "get_posts()" in the functions. So what I did was basically if $_GET(['post_per_page']) was set and not empty, I simply added it to the array
    – envysea
    Commented May 16, 2014 at 21:14
  • brilliant solution :)
    – Rares P.
    Commented Dec 5, 2014 at 13:18
  • staging.thewindow.barneys.com/api_v2/city/get_taxonomy_posts/… hi i am getting this when i tried above,what could be the problem,please help
    – Melvin
    Commented Feb 25, 2016 at 9:28

I have just tested this (I am doing the exact same thing for a project) and it worked fine for me.

Added tags to a custom post type, ran this:


And the json file looked like so:

        "title":"custompost 12",
        "title_plain":"custompost 12",
        "date":"2011-05-26 12:32:59",
        "modified":"2011-05-26 13:54:03",

Are you sure that you added the custom taxonomies to the Custom Post Type?

add_action( 'init', 'create_my_post_types' );   
function create_my_post_types() {
    register_post_type( 'custompost',
            'taxonomies' => array( 'category', 'post_tag' ),

If you are looking to do something more complex, like define a hierarchical structure of custom taxonomies, this link might help you.

  • downvotes with no comments? whaddid I do wrong here exactly?
    – edzillion
    Commented Jun 2, 2011 at 19:43
  • /?json=get_recent_posts&post_type=custompost worked for me, thanks!
    – IvanRF
    Commented Oct 8, 2014 at 23:20

There ARE hooks available. http://wordpress.org/extend/plugins/json-api/other_notes/#Filter:-json_api_controllers

  • can you write an example to add custom taxonomy support? Commented May 25, 2011 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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