最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

REST API retrieving posts from www.sitename.comcategorynews instead of just just from www.sitename.com

programmeradmin8浏览0评论

I look a bit thorough some of the previously asked questions and stumbled upon /wp-json/wp/v2/pages/?slug=news (and other permutations) but it doesn't turn anything up. I'm kind of stuck on how to get posts for each category. I looked at /wp-json/wp/v2/ for trying to get a grasp on how routes work and looked at categories, but couldn't figure it out. I also checked out /wp/v2/posts?filter[category_name]=news but it just ends up being the same post no matter what I change the category name to.

I look a bit thorough some of the previously asked questions and stumbled upon /wp-json/wp/v2/pages/?slug=news (and other permutations) but it doesn't turn anything up. I'm kind of stuck on how to get posts for each category. I looked at /wp-json/wp/v2/ for trying to get a grasp on how routes work and looked at categories, but couldn't figure it out. I also checked out /wp/v2/posts?filter[category_name]=news but it just ends up being the same post no matter what I change the category name to.

Share Improve this question edited May 30, 2019 at 7:21 onibaku asked May 30, 2019 at 7:05 onibakuonibaku 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If you want to retrieve posts, you need to use the posts endpoint for starters, not pages. So that's wp-json/wp/v2/posts. The documentation for that endpoint is here.

As you can see in the documentation, under List Posts, you can retrieve posts for a specific category using the categories argument. However, you need to use the category ID, not the slug.

http://example/wp-json/wp/v2/posts/?categories=14

If you absolutely must use the slug, then you need to retrieve that first, from the categories endpoint:

http://example/wp-json/wp/v2/categories/?slug=news

That will retrieve any categories with news as the slug. This response will include the category ID which you can store and use to query posts with that category.

发布评论

评论列表(0)

  1. 暂无评论