5

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:

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?

2

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.

  • Ya I found out why it wasn't working I need to reference the 'id' of the category now I need to figure out how to get 100 posts after the first 100. The API won't do me any good if I can't figure out how to successfully pull these posts for a react powered theme. – DᴀʀᴛʜVᴀᴅᴇʀ Jul 18 '18 at 4:36
  • 100 at a time is a lot of posts. It's the max but that doesn't mean its a good idea to query all of them at once. Either way, you combine per_page with page. So per_page=20 will get the first 20, then per_page=20&page=2 will get the next 20. It's the responsibility of your application to keep track of whether there's enough posts for a 2nd page. – Jacob Peattie Jul 18 '18 at 4:46
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.