我想在WP-API中按类别搜索帖子。
我知道我可以通过属性类别或filtercat搜索帖子。
但这些职位包含不止一个类别。
我试着这样搜索:
{host}/wp-json/wp/v2/posts?categories=69&filter[cat]=[228,246,237]&per_page=50或
{host}/wp-json/wp/v2/posts?categories=69&filter[cat]=228&filter[cat]=246&filter[cat]=237&per_page=50或
{host}/wp-json/wp/v2/posts?categories=69&categories=246&categories=237&categories=228这对我没用。它导致搜索寻找最后一个属性。
有什么想法吗?
这是Json响应的结构。
{
"id": 9333,
"date": "2016-08-02T14:17:01",
"date_gmt": "2016-08-02T12:17:01",
"guid": {
"rendered": "{post}/?p=9333"
},
"modified": "2016-08-03T08:50:35",
"modified_gmt": "2016-08-03T06:50:35",
"slug": "{post}",
"type": "post",
"link": "{host}/{post}/",
"title": {
"rendered": "{post}"
},
"content": {
"rendered": "{post}"
},
"excerpt": {
"rendered": "{post}"
},
"author": 3,
"featured_media": 0,
"comment_status": "closed",
"ping_status": "closed",
"sticky": false,
"format": "standard",
"categories": [
228,
237,
207,
217,
246,
231,
69,
221,
270,
244
],
"tags": [],
"_links": []
}谢谢!
发布于 2016-08-14 19:58:18
如果您想获得来自多个类别的帖子,根据您的需求,几乎没有解决方案。
如果您想从ID =1或类别ID =2的类别中获取帖子,请使用以下URL:
http://localhost/lifelog/wp-json/wp/v2/posts?filter[cat]=1,2或者:
http://localhost/lifelog/wp-json/wp/v2/posts?categories=1,2如果您希望从ID =1的类别中获取帖子,而从ID =2的类别中获取帖子,则可以使用:
http://localhost/lifelog/wp-json/wp/v2/posts?filter[category__and][]=1&filter[category__and][]=2但是-在筛选器数组中使用的一些筛选值需要具有edit_posts权限的身份验证用户。
幸运的是,有一个更简单的解决方案-- WordPress支持以下链接:
http://example.com/category/test1+test2/在上述网址下,您将得到分配给test1和test2类别的帖子列表。在REST中,您可以使用以下URL实现相同的行为:
http://localhost/lifelog/wp-json/wp/v2/posts?filter[category_name]=test1%2Btest2请记住,您必须将+符号替换为%2B。
https://stackoverflow.com/questions/38944843
复制相似问题