Json wpapi按Post模式筛选

Json wpapi按Post模式筛选,json,wordpress,wp-api,Json,Wordpress,Wp Api,是否可以根据其模式从Wordpress Rest API v2返回帖子列表: 对于架构列表: 我想通过粘性字段进行过滤,但对其余字段也是如此 到目前为止,我已经: /wp-json/wp/v2/posts?filter[sticky]=true /wp-json/wp/v2/posts?filter[sticky]=1 两者都返回与标准端点相同的响应: /wp-json/wp/v2/posts 我读过其他材料,如详细说明如何按或自定义排序,但我不认为这与此相同。在查看文档并在WP-API

是否可以根据其模式从Wordpress Rest API v2返回帖子列表:

对于架构列表:

我想通过粘性字段进行过滤,但对其余字段也是如此

到目前为止,我已经:

/wp-json/wp/v2/posts?filter[sticky]=true
/wp-json/wp/v2/posts?filter[sticky]=1
两者都返回与标准端点相同的响应:

/wp-json/wp/v2/posts

我读过其他材料,如详细说明如何按或自定义排序,但我不认为这与此相同。

在查看文档并在WP-API Github回购上查找和发布问题后,很明显,
过滤器[忽略粘性帖子]
应该切换预期的排序行为,因此,粘性帖子要么总是第一个(默认),要么被忽略(通过使用
过滤器[ignore\u sticky\u posts]=true

但是,有一个问题使得
过滤器[忽略粘贴的帖子]
标志不可用

现在修复它的最好方法是获取数据库中所有粘性帖子的数据或ID。通过查看中的代码和,我认为将以下代码添加到
functions.php
应该可以做到这一点:

// Sticky posts in REST - https://github.com/WP-API/WP-API/issues/2210
function get_sticky_posts() {
    $posts = get_posts(
        array(
            'post__in' => get_option('sticky_posts')
        )
    );

    if (empty($posts)) {
        return null;
    }

    return $posts;
}
add_action( 'rest_api_init', function () {
    register_rest_route( 'THEME_NAME/v1', '/sticky', array(
        'methods' => 'GET',
        'callback' => 'get_sticky_posts',
    ));
});
如果您
获取
/wp json/THEME\u NAME/v1/sticky,您应该会获得所有sticky帖子的数组


我希望这会有所帮助。

除了Laust Deleuran的答案(谢谢Laust!),我还创建了他的脚本的一个修改版本,允许您使用
REST api的
嵌入式
功能

尽管这可能不是“最干净”的解决方案,但它确实允许您充分使用
wp-json
的功能


function get_sticky_posts(WP_REST_Request $request) {

    $request['filter'] = [
        'post__in' => get_option('sticky_posts')
    ];

    $response = new WP_REST_Posts_Controller('post');
    $posts = $response->get_items($request);

    return $posts;
}

add_action( 'rest_api_init', function () {
    register_rest_route( 'THEME_NAME/v1', '/sticky', array(
        'methods' => 'GET',
        'callback' => 'get_sticky_posts',
    ));
});


这将在与正常的
/wp json/wp/v2/posts
查询响应相同的
模式中输出粘性
posts

显然,它应该通过
过滤器[ignore_sticky_posts]=true | false
实现,但在Beta11中这对我不起作用。正在进行的调查: