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