duandi4815 2016-08-14 17:39
浏览 51
已采纳

按类别搜索帖子Wordpress WP-API

I want to search posts by categories in the WP-API.

I know I can search posts by the attribute categories or filter[cat].

But the posts contains more than one category.

I tried to search like this:

{host}/wp-json/wp/v2/posts?categories=69&filter[cat]=[228,246,237]&per_page=50

or

{host}/wp-json/wp/v2/posts?categories=69&filter[cat]=228&filter[cat]=246&filter[cat]=237&per_page=50

or

{host}/wp-json/wp/v2/posts?categories=69&categories=246&categories=237&categories=228

This didn't work for me. It caused the search to look for the last attribute.

Any ideas?

This is the structure of Json response

{
    "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": []
  }

Thanks!

  • 写回答

1条回答 默认 最新

  • duanmengmiezen8855 2016-08-14 19:58
    关注

    If you want to get posts from multiple categories, there are few solutions, depending from your needs.

    In case when you want to get posts from category with ID = 1 OR category with ID = 2 use the following URL:

    http://localhost/lifelog/wp-json/wp/v2/posts?filter[cat]=1,2
    

    or:

    http://localhost/lifelog/wp-json/wp/v2/posts?categories=1,2
    

    For case when you want to get posts from category with ID = 1 AND category with ID = 2 you can use:

    http://localhost/lifelog/wp-json/wp/v2/posts?filter[category__and][]=1&filter[category__and][]=2
    

    BUT - some filter values used in the filter array needs authenticated user with edit_posts privileges.

    Fortunately, there is a more simple solution - WordPress supports links like:

    http://example.com/category/test1+test2/
    

    Under above URL you will get list of posts which are assigned to the test1 AND test2 categories. And in the REST API you can achieve the same behaviour with the following URL:

    http://localhost/lifelog/wp-json/wp/v2/posts?filter[category_name]=test1%2Btest2
    

    Please remember that you have to replace the + sign into %2B.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上