从WordPress 4.7开始,我们现在有以下要挂接的过滤器:
$args = apply_filters( "rest_{$this->post_type}_query", $args, $request );
因此,如果您想执行如下请求:
http://yoursite.com/wp-json/wp/v2/posts?meta_key=your_key&meta_value=your_value&per_page=10
或
http://yoursite.com/wp-json/wp/v2/posts?meta_query[0][key]=your_key&meta_query[0][value]=your_value&per_page=10
您可以通过以下代码段(在functions.php中)完成此操作:
/**
* This function will allow custom parameters within API request URL.
*
* @link https://codex.wordpress.org/Class_Reference/WP_Query
* @see Wp-includes/Rest-api/Endpoints/Class-wp-rest-posts-controller.php
* @param array $args Contains by default pre written params.
* @param array $request Contains params values passed through URL request.
* @return array $args New array with added custom params and its values.
*/
public function posts_allow_custom_request_params( $args, $request ) {
$args += array(
\'meta_key\' => $request[\'meta_key\'],
\'meta_value\' => $request[\'meta_value\'],
\'meta_query\' => $request[\'meta_query\'],
);
return $args;
}
add_filter( \'rest_post_query\', \'posts_allow_custom_request_params\', 99, 2 );
请注意,我假设您正在从post类型“post”请求post。
只要数组索引与WP_Query 班如果您查看我的注释以及代码,您可能会发现一些有用的内容。
顺便说一下,以下是默认情况下可以在查询字符串中使用的一些参数:
\'author\' => \'author__in\',
\'author_exclude\' => \'author__not_in\',
\'exclude\' => \'post__not_in\',
\'include\' => \'post__in\',
\'menu_order\' => \'menu_order\',
\'offset\' => \'offset\',
\'order\' => \'order\',
\'orderby\' => \'orderby\',
\'page\' => \'paged\',
\'parent\' => \'post_parent__in\',
\'parent_exclude\' => \'post_parent__not_in\',
\'search\' => \'s\',
\'slug\' => \'post_name__in\',
\'status\' => \'post_status\',
\'per_page\' => \'posts_per_page\',
考虑到WP\\u查询类,“page”等效于“paged”,“status”等效于“post\\u status”,依此类推。。。
使用上述参数,我们最终得到如下结果:
http://yoursite.com/wp-json/wp/v2/posts?offset=2&status=future
希望这有帮助。并且要乐观地相信,如果他们这样做了,那将永远是最好的!