tho*_*ers 10

发现它,有点隐藏

使用类别ID: /posts?filter[cat]=1

使用类别名称: /posts?filter[category_name]=MyCategory

  • 改变了v2`&categories = 1` (7认同)
  • 自Wordpress 4.7起,它似乎已从API中删除了wordpress的过滤器。@TomWoodward的答案有效。 (2认同)

小智 5

对于类别名称,应添加两个过滤器,如下所示:

add_filter( "rest_post_query", function( $args, $request){
                if ( isset( $request['category_name']) && !empty($request['category_name'] ) ) {
                    $args['category_name'] = $request['category_name'];
                }
                return $args;
            }, 10, 2);


add_filter( "rest_post_collection_params", function($query_params, $post_type){
                $query_params[ 'category_name' ] = array(           
                    'description' => __( 'Category name.' ),
                    'type'        => 'string',
                    'readonly'    => true,
                );
                return $query_params;
            }, 10, 2);
Run Code Online (Sandbox Code Playgroud)


Mat*_*osa 5

这个问题与论坛上的另一个问题重复

http://example.com/wp-json/wp/v2/posts?categories=20,30
Run Code Online (Sandbox Code Playgroud)

以上将返回来自 category 20 OR category 30

我已经使用自定义帖子类型进行了测试,它也可以完美运行

回应和学分归“Manish Jung Thapa”所有