我不熟悉JSON API,但这对我来说很有用。
这个答案的灵感来自https://wordpress.stackexchange.com/a/18200以及关于JSON API外部控制器的文档,如下所述:here
首先创建控制器文件mikictrl。php,在您的主题目录中。
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
);
}
}
然后将以下内容添加到主题的功能中。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\';
}
最后,转到wordpress admin中的JSON API,并启用Mikictrl控制器。
现在,您可以按自定义字段的meta\\u键对查询进行排序:
http://example.com/api/Mikictrl/get_custom_posts/?key=_yourcustomfieldkey&custom_fields=_yourcustomfieldkey&order=desc&include=title,custom_fields&dev=1
此外,如果填写value参数,则可以按meta\\u值进行筛选:
http://example.com/api/Mikictrl/get_custom_posts/?key=_yourcustomfieldkey&value=yourcustomfieldvalue&custom_fields=_yourcustomfieldkey&order=desc&include=title,custom_fields&dev=1