这里的解决方案分为两部分。
您需要使用JSON API custom controller在自定义控制器中,您需要决定如何通过meta_query
数据结构取决于您需要的健壮性,您可以使用多种方法。这是最大化方法,它允许meta_query
, 将结构编码为JSON字符串。
<?php
// 1. The class name must match the filename (i.e., "foo.php" for JSON_API_Foo_Controller)
// 2. Save this in your themes folder
// 3. Activate your controller under Settings > JSON API
class JSON_API_Example_Controller {
public function get_posts_by_meta_query() {
global $json_api;
if (empty($_GET[\'meta_query\'])) {
return array(
\'error\' => "Specify a \'meta_query\' param."
);
}
$query = array(
\'ignore_sticky_posts\' => true,
\'numberposts\' => -1,
\'post_type\' => \'event\',
\'meta_query\' => json_decode($_GET[\'meta_query\'], true)
);
return array(
\'posts\' => $json_api->introspector->get_posts($query)
);
}
}
?>
所以如果你想通过这个
meta_query
作为URL编码的JSON:
$args = array(
\'numberposts\' => -1,
\'post_type\' => \'event\',
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'place\',
\'value\' => \'Melbourne\'
),
array(
\'key\' => \'dayoweek\',
\'value\' => \'saturday\'
)
)
);
用JavaScript对其进行如下编码:
var meta_query = encodeURIComponent(JSON.stringify({
relation: \'AND\',
0: {
key: \'place\',
value:\'Melbourne\'
},
1: {
key: \'dayoweek\',
value: \'saturday\'
}
}));
以下是URL查询参数的外观(有点笨拙):
/?json=example/get_posts_by_meta_query&meta_query=%7B%220%22%3A%7B%22key%22%3A%22place%22%2C%22value%22%3A%22Melbourne%22%7D%2C%221%22%3A%7B%22key%22%3A%22dayoweek%22%2C%22value%22%3A%22saturday%22%7D%2C%22relation%22%3A%22AND%22%7D