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

Wordpress REST API parameters are not affecting a response

programmeradmin2浏览0评论

I'm working on small, vue based SPA. I'm using axios to get data from api that I'm interested about. The problem is that parameters are not working in any way, the same happens in Postman (I tried Postman because I thought that there is an error in my code, but there is not). Let's say I want to get posts from category 12 and order them ascending. So what I would try is something like this:

/?categories=12&order=asc

Call presented above returns posts in wrong order. I'm struggling with this and can't make it work - so frustrating that official documentation lacks real usage examples...

I'm working on small, vue based SPA. I'm using axios to get data from api that I'm interested about. The problem is that parameters are not working in any way, the same happens in Postman (I tried Postman because I thought that there is an error in my code, but there is not). Let's say I want to get posts from category 12 and order them ascending. So what I would try is something like this:

http://api-address/wp-json/wp/v2/posts/?categories=12&order=asc

Call presented above returns posts in wrong order. I'm struggling with this and can't make it work - so frustrating that official documentation lacks real usage examples...

Share Improve this question edited Jun 26, 2019 at 10:16 Sebastian asked Jun 26, 2019 at 8:21 SebastianSebastian 13 bronze badges 3
  • 1 What's incorrect about the response you're receiving? You say it "just returns all posts from category with id=12", which sounds correct to me. Is the order wrong? By default posts are ordered by date, so this would return posts with the oldest first. Is that not what you're getting? – Jacob Peattie Commented Jun 26, 2019 at 9:46
  • Thank You for Your reply. That was a mistake, I mean that those posts are not sorted in the way I want (ascending). – Sebastian Commented Jun 26, 2019 at 10:15
  • 2 When you say it's in the wrong order, what order is it in? Is it in descending order by date? Or something else? The only reason it wouldn't be sorting correctly is if you have a theme, plugin or other custom code that affects the ordering of posts. Do you still have the issue with a default theme and no plugins active? – Jacob Peattie Commented Jun 26, 2019 at 10:19
Add a comment  | 

2 Answers 2

Reset to default 1

If results should be ordered by a method other than the default asc or desc, you should also pass the orderby parameter. This gives you the ability to order by title, date, parent, type and many more that can be seen here.

For example, a request ordering the results a-z by title would be:

http://api-address/wp-json/wp/v2/posts/?categories=12&order=asc&orderby=title

@thomas answer is right Sometimes is parameter don't recognised by the API then it will be give the default response.

You need to provide some default argument to the API.

You can use your own custom rest route to the your site.

Sometimes you need authentication from the word wordpress like WP nouns and anothers. For POST request.

Wordpress rest API 2 support delete get post put.

In WordPress Codex you might get stuck.

发布评论

评论列表(0)

  1. 暂无评论