Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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?

share|improve this question
    
Does the plugin support standard WP_Query arguments? –  sanchothefat Jan 3 '13 at 15:51
add comment

3 Answers

Looking through the source code, this plugin maps the query variable orderby to the WP_Query argument order_by.

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

share|improve this answer
    
Thanks @peter, unfortunately that's not working for me –  Benny Jan 11 '13 at 1:10
add comment

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.

share|improve this answer
    
Thanks bueltge. I failed to mention in the question that the results will be pages, so the sorting will have to happen server side. I'm hoping to find a method that doesn't involve me modiying php server side, but if that's what I need to do then so be it. Thanks again. –  Benny Dec 22 '12 at 4:02
    
The javascript way is not enough? –  bueltge Jan 3 '13 at 12:26
    
Nope. The JSON array doesn't contain all of the data at any one point, it comes to the client in multiple requests. It's an Android / IOS app, with a pull to refresh + continuous scroll view. Once the list is scrolled to the bottom, more data is sent from the server. This is why it must be sorted server side. –  Benny Jan 4 '13 at 1:02
    
Sorry, I dont understand this right. You can sort on server side with js or php, like the functions from the answer. Also you can use the URL, like this form your question and filter this in all parameters with the php function parse_url. Also you can sort with java, but not very usefull with WordPress. Maybe you read this post, is a example of sorting from JSON on server with js and php. –  bueltge Jan 4 '13 at 9:05
add comment

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

This answer is inspired by http://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
share|improve this answer
add comment

Your Answer

 
discard

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.