Sorting results from JSON-API on custom fields

Is there any way, out of the box, to sort results from the JSON-API plugin based on values in custom fields? The request is paged, so the results will need to be sorted server-side.

I have an http request along the lines of:

Read More
http://www.example.com/wordpress/?json=get_author_posts&author_slug=user&post_type=custom
&include=title,custom_fields&custom_fields=date_value&count=10&page=1

I’d like to sort on the custom field date_value, ideally without having to create a new controller within the json plugin. Is this possible?

Related posts

Leave a Reply

3 comments

  1. Looking through the source code, this plugin maps the query variable orderby to the WP_Query argument of the same name, orderby.

    What this means is that you should be able to do the following:
    http://www.example.com/wordpress/?json=get_author_posts&author_slug=user&post_type=custom&include=title,custom_fields&custom_fields=date_value&count=10&page=1&orderby=date_value

  2. You can sort via javascript .sort() a json array.

    Your example string was also splitable via php, like explode()

    explode( '&', $your_string );
    

    or parse_url

    For sorting a JSON with php use usort; do you find solutions via G* search.

    Also you can use json_decode(), my favorite way, and create a php array from the json object and use on of different php functions to sort this array.

  3. I’m new to JSON API but this worked for me.

    This answer is inspired by https://wordpress.stackexchange.com/a/18200
    and documentation about JSON API external controller as described: here

    First create your controller file mikictrl.php, in your theme directory.

    class JSON_API_Mikictrl_Controller {
    
      public function get_custom_posts() {
      global $json_api;
    
      // See also: http://codex.wordpress.org/Template_Tags/query_posts
      $posts = $json_api->introspector->get_posts(array(
        'meta_key' => $json_api->query->key,
        'meta_value' => $json_api->query->value,
        'orderby' => $json_api->query->key
      ));
    
      return array(
        'key' => $json_api->query->key,
        'value' => $json_api->query->value,
        'posts' => $posts
      );
     }
    }
    

    Then 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[] = 'Mikictrl';
      return $controllers;
    }
    
    // Register the source file for our controller
    add_filter('json_api_mikictrl_controller_path', 'mikictrl_controller_path');
    function mikictrl_controller_path($default_path) {
      return get_stylesheet_directory() . '/mikictrl.php';
    }
    

    And last, go to JSON API in wordpress admin, and enable the Mikictrl controller.

    Now you can sort a query by meta_key of your custom fields :

    http://example.com/api/Mikictrl/get_custom_posts/?key=_yourcustomfieldkey&custom_fields=_yourcustomfieldkey&order=desc&include=title,custom_fields&dev=1
    

    Also, you can filter by a meta_value if you fill the value parameter :

    http://example.com/api/Mikictrl/get_custom_posts/?key=_yourcustomfieldkey&value=yourcustomfieldvalue&custom_fields=_yourcustomfieldkey&order=desc&include=title,custom_fields&dev=1