15

I want to get the count of my posts. I think use

wp-json/wp/v2/categories?page=1 >> count it's a good way to go. But using above solution is not a cup of tea in term of speed and i don't know how to get all the categories by one call.(if u know it would be so helpful if u share it). is there any way/tutorial/... enable me to add the post count in a main call? like this route: wp-json/wp/v2/posts....

4 Answers 4

31

The WP Rest API sends the total count(found_posts) property from WP_Query. in a header called X-WP-Total.

FOR POSTS: you can make a call to posts endpoint of the REST API

http://demo.wp-api.org/wp-json/wp/v2/posts

The value for posts count is returned in the header as X-WP-Total. Below is a sample response from the hosted demo

Access-Control-Allow-Headers:Authorization, Content-Type
Access-Control-Expose-Headers:X-WP-Total, X-WP-TotalPages
Allow:GET
Cache-Control:max-age=300, must-revalidate
Connection:keep-alive
Content-Encoding:gzip
Content-Type:application/json; charset=UTF-8
Date:Wed, 28 Dec 2016 12:48:50 GMT
Last-Modified:Wed, 28 Dec 2016 12:48:50 GMT
Link:<https://demo.wp-api.org/wp-json/wp/v2/posts?page=2>; rel="next"
Server:nginx/1.4.6 (Ubuntu)
Transfer-Encoding:chunked
Vary:Cookie
Via:1.1 dfa2cbb51ec90b28f03125592b887c7d.cloudfront.net (CloudFront)
X-Amz-Cf-Id:ri4C3e-AdixwqGv_wYNdGRq9ChsIroy1Waxe2GqkiTqbk4CpiSIQfw==
X-Batcache:MISS
X-Cache:Miss from cloudfront
X-Content-Type-Options:nosniff
X-EC2-Instance-Id:i-198c7e94
X-Powered-By:PHP/7.0.11-1+deb.sury.org~trusty+1
X-Robots-Tag:noindex
X-WP-Total:71
X-WP-TotalPages:8

NOTE:

You can also limit the posts per page you're fetching to 1 so you're not getting all your wordpress posts just to get the posts count

http://demo.wp-api.org/wp-json/wp/v2/posts?per_page=1

To Get All Categories

All you have to do is make a GET request to the categories endpoint at:

http://demo.wp-api.org/wp-json/wp/v2/categories

This would return all the categories and also the total count of categories can be found in the X-WP-Total header.

8

In case it's not clear how to actually access the header (with the post count) from the Ajax request, following is how to do it with jQuery .get. The key is that the callback function has an optional parameter containing the request, which includes the headers.

$.get( 'http://demo.wp-api.org/wp-json/wp/v2/posts', function( data, status, request ) {
numPosts = request.getResponseHeader('x-wp-total');
console.log( numPosts ); //outputs number of posts to console
});
1

Try to answer to part of your question. Count of post wp rest api v2 returned in headers. You can get value something like this:

headers('X-WP-Total')

Count posts per page:

wp-json/wp/v2/categories?page=1&per_page=5
0

I've spent hours on searching on getting number of posts with WP REST API even v2 through Google and WordPress or the library official docs but unfortunately came up with nothing.

So I tried getting posts with increasing page number until the returned JSON array length becomes 0 which means there's no more page with posts on the category, and stopped increasing the page number.

But if you want to get an arbitrary number of posts per page with WP REST API you can easily use filter[posts_per_page]={$numberOfPosts} parameter while sending the request.

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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