wordpresspagination

per_page parameter does not work for WordPress REST API v2


When calling WordPress REST API v2, the per_page parameter is totally ignored.

Expected behaviour for https://gao.bo/asahi/wp-json/wp/v2/posts?per_page=1 is to return only 1 post however it returns 10 posts, which is the default for pagination set by WordPress. Seems that it completely ignores the GET parameter given.

https://gao.bo/asahi is a subsite for https://gao.bo by the way.

The site is in Simplified Chinese but I don't think that will affect the API behaviour right?

WordPress version: 6.5.3

All other GET parameters work as expected. For instance:

https://gao.bo/asahi/wp-json/wp/v2/posts?per_page=1&page=3 Shows the third page but still 10 posts per page.

https://gao.bo/asahi/wp-json/wp/v2/posts?per_page=1&page=3&_fields=id,link Shows the third page with only id and link of each post but still 10 posts per page.

In a word, per_page is the only GET parameter which gets ignored.


Solution

  • I can't say what the source of the issue is, but I think it might be an issue with a CDN or WAF in front of the server. If I alter the URL just a little bit, I'm able to get 1 post per page:

    https://gao.bo/asahi/wp-json/wp/v2/posts?&per_page=1

    If I were trying to figure out the problem, I'd be looking at what's in front of your WordPress site: CDN, WAF, or hosting platform. If you have plugins that are designed for security or other preprocessing of HTTP requests, those might be worth looking at also.

    Some additional detail that might help is that it's not the per_page parameter that has an issue, it's just whatever the first parameter is. Try this:

    https://gao.bo/asahi/wp-json/wp/v2/posts?page=2&per_page=1

    You get one post per page, but it's ignoring the page parameter because it's the first one.