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?
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
You can sort via javascript
.sort()
a json array.Your example string was also splitable via php, like
explode()
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.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.
Then add following to your theme’s functions.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 :
Also, you can filter by a meta_value if you fill the value parameter :