To start I’ve been searching through several questions and documentation pieces but it seems with WordPress’ v2 many of the old questions are no longer valid. What I am trying to do is get all posts or a singular post from a category in Postman instead of the common returned 10 posts without having to modify the API in functions.php.

tldr

I started with referencing the REST API Handbook and reviewing the schema I saw categories and I can return the 10 latest categories using:

http://foobar.com/wp-json/wp/v2/posts/categories_name=hello

referencing the arguments I see per_page so I tried:

http://foobar.com/wp-json/wp/v2/posts/categories_name=hello?per_page=‌​1

and it returns 10 posts from the category hello so I modified and tried:

http://foobar.com/wp-json/wp/v2/posts/categories_name=hello&per_page=‌​1

and I get an error returned:

{
“code”: “rest_invalid_param”,
“message”: “Invalid parameter(s): per_page”,
“data”: {
“status”: 400,
“params”: {
“per_page”: “per_page is not of type integer.”
}
} }

Searching through Google I see How to retrieve a list of categories/ tag in WordPress REST API but the answers are based on v1.

Trying WordPress API JSON return limit I use:

http://foobar.com/wp-json/wp/v2/posts/?per_page=‌​1

and I get a singular post so I modified my attempt to:

http://foobar.com/wp-json/wp/v2/posts/?per_page=‌​1$categories_name=hello

It ignores the category type and returns the latest post. Reading Get more than 10 posts in a specific category with the WordPress API I pulled the ID of a category (4) after using:

http://foobar.com/wp-json/wp/v2/categories

then coded:

http://foobar.com/wp-json/wp/v2/posts/?categories=4&per_page=‌​1

and I get:

{
“code”: “rest_invalid_param”,
“message”: “Invalid parameter(s): per_page”,
“data”: {
“status”: 400,
“params”: {
“per_page”: “per_page is not of type integer.”
}
} }

I thought I might be able to use -1 similar to the development of a theme but I get an error.

Other references I read are:

  • WP REST API – Retrieve content from page
  • WP REST API Category
  • Found this Since filter has been removed, how to get posts by category slug with same schema as v2/posts? after reading Search post by categories WordPress WP-API

  • How to get all posts related to particular category name?

After reviewing the documentation on Pagination it seems to only work with the post and not any category. I can only get more than 10 posts if I use /wp-json/wp/v2/posts?per_page=20, too.

Question

When calling a site’s WP API how can I control the per_page return of a category wether it be 1 post or all posts?

3 Answers
3

Pretty much all the URLs you are using are invalid in some way:

http://foobar.com/wp-json/wp/v2/posts/categories_name=hello

categories_name is not a valid argument for listing posts, and even if it was you are missing the ? part of the query string.

http://foobar.com/wp-json/wp/v2/posts/categories_name=hello?per_page=‌​1

This one is also missing the ?. Query parameters on a URL, which the API uses for its arguments, need to start with ?, with & for additional parameters. A correctly formatted query string looks like this:

http://domain.com/path/?argument=value&argument2=value2

So this one:

http://foobar.com/wp-json/wp/v2/posts/categories_name=hello&per_page=‌​1

Is also missing the ? but you’ve used & correctly this time (though are still using the invalid categories_name argument).

This one:

http://foobar.com/wp-json/wp/v2/posts/?per_page=‌​1$categories_name=hello

Is using a $ for some reason. That’s not a valid way to separate parameters in a query string (and still using the invalid categories_name argument).

This one is correct:

http://foobar.com/wp-json/wp/v2/posts/?categories=4&per_page=‌​1

But based on your comment:

I thought I might be able to use -1 similar to the development of a
theme but I get an error.

It sounds like you actually tried:

http://foobar.com/wp-json/wp/v2/posts/?categories=4&per_page=‌​-1

Which won’t work, because -1 is an invalid value. You can only retrieve between 1 and 100 results with the API.

Leave a Reply

Your email address will not be published. Required fields are marked *