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:
referencing the arguments I see per_page
so I tried:
=1
and it returns 10 posts from the category hello
so I modified and tried:
;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:
/?per_page=1
and I get a singular post so I modified my attempt to:
/?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:
then coded:
/?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?
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/wp-json/wp/v2/posts/categories_name=hello
referencing the arguments I see per_page
so I tried:
http://foobar/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/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/wp-json/wp/v2/posts/?per_page=1
and I get a singular post so I modified my attempt to:
http://foobar/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/wp-json/wp/v2/categories
then coded:
http://foobar/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
Reset to default 3Pretty much all the URLs you are using are invalid in some way:
http://foobar/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/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/path/?argument=value&argument2=value2
So this one:
http://foobar/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/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/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/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.
After a few hours trying different permutations of how to get a return on categories I found my answer. To establish the listing of categories associated with post
use:
/wp-json/wp/v2/categories/
I needed a particular category and the id
of that category was 4
and the slug was called foobar
.
To get 1 post associated with the category foobar
I had to use:
/wp-json/wp/v2/posts?categories=4&per_page=1
To get 100 posts associated with the category foobar
I used:
/wp-json/wp/v2/posts?categories=4&per_page=100
and as I found out and it was also mentioned in the answer my logic of using -1
was incorrect, you can only use 1 to 100. As it was discussed in the comment on what to do if there are more than 100 posts, well you can call the page using offset
and I found that under the documentation Pagination:
/wp-json/wp/v2/posts?categories=4&offset=10&per_page=1
so to get more than 100 you'd to build a loop in whatever language you wanted and push it to a file. To assist with the looping under /wp-json/wp/v2/categories/
and when you reference the category you can reference the total count under count
which is below the id
.
When doing "per_page" on the products/categories endpoint, the MAX you can use is 100. Anything more then that will cause the error Error: Invalid parameter(s): per_page [rest_invalid_param]