经过大量的挖掘,我似乎自己找到了一个解决方案,一个非常强大的解决方案,它利用了rest_{ post_type }_query
筛选器(将{post\\u type}部分替换为自定义post类型的slug。您可以使用rest_post_query
更改默认值/wp-json/wp/v2/posts
请求):
public function __construct() {
// Make sure you add those numbers at the end of the line, or it won\'t work
add_filter( \'rest_opps_query\', array( $this, \'get_opps_by_state\' ), 10, 2 );
}
public function get_opps_by_state( $args, $request ) {
$state = $request->get_param( \'state\' );
if( $state == \'open\' ) {
$args[ \'meta_query\' ] = array(
\'deadline\' => array(
\'key\' => \'deadline\',
\'value\' => date( \'Ymd\' ),
\'compare\' => \'>\',
\'type\' => \'DATE\'
)
);
} elseif ( $state == \'closed\' ) {
$args[ \'meta_query\' ] = array(
\'deadline\' => array(
\'key\' => \'deadline\',
\'value\' => date( \'Ymd\' ),
\'compare\' => \'<\',
\'type\' => \'DATE\'
)
);
}
return $args;
}
因此,现在我可以使用默认的Wordpress API路由来获取自定义帖子:
http://example.com/wp-json/wp/v2/opps
并根据自定义参数筛选结果,
status
:
http://example.com/wp-json/wp/v2/opps?status=open
http://example.com/wp-json/wp/v2/opps?status=closed
无需自定义路线!