How to get total number of posts in WP Rest Api? - wordpress

i want to know the total number of posts in my Wordpress Website using rest api, i can see total number of posts in json using this..
https://www.punjabidharti.com/?json=get_recent_posts
the the result is like this..
{"status":"ok","count":7,"count_total":34470,"pages":4925,"posts"
but when i use rest api to fetch data i can't see total number of posts.. this is the url to fetch data using rest api
https://www.punjabidharti.com/wp-json/wp/v2/posts/
is there any way to show total number of posts in rest api too ?

Yes, it is possible to retrieve the total number of posts using the WordPress REST API.
The total number of posts can be obtained by making a GET request to the /wp/v2/posts endpoint and passing the per_page parameter with a value of 1. (https://www.punjabidharti.com/wp-json/wp/v2/posts?per_page=1)
This will return only one post, but it will also include the X-WP-Total and X-WP-TotalPages headers in the response, which will give you the total number of posts and the total number of pages, respectively.
The response will include the X-WP-Total and X-WP-TotalPages headers, which you can use to get the total number of posts and the total number of pages. In your case, your headers will be :
X-WP-Total: 34470
X-WP-TotalPages: 4925

Related

WooCommerce Rest API not returning Products

So i am trying to get all products from woocommerce rest api but it keeps on returning empty result like []. When I give the same API a product ID, it returns proper result but when I remove the product ID, it returns me nothing.
This is what works
https://example.com/wp-json/wc/v3/products/{PRODUCT-ID}?lang=en&consumer_key=ck_xxxxxxxxx&consumer_secret=cs_xxxxx
But when I try to get all products like this
https://example.com/wp-json/wc/v3/products?lang=en&consumer_key=ck_xxxxxxxxx&consumer_secret=cs_xxxxx
I keep on getting []
How can I get all the products from the rest API?
Try without param ?lang=en.
https://example.com/wp-json/wc/v3/products?consumer_key=ck_xxxxxxxxx&consumer_secret=cs_xxxxx.
Try the following:
https://example.com/wp-json/wc/v3/products?page=1&orderby=id&order=asc
That should give you the first 10 products as it usually goes 10 products per page (there are ways to request more, up to 100). Then you can go for the next page and the next page until they all download.

Get All Categories from Woocommerce using REST API

I am trying to retrieve all categories using Woocommerce rest API.
here is my URL
https://wapi.sriyagcommerce.com//wp-json/wc/v2/products/categories?consumer_key=ck&consumer_secret=cs&per_page=100
while using above url I am getting Error.
code "rest_invalid_param"
message "Invalid parameter(s): per_page"
data
status 400
params
per_page "per_page must be between 1 (inclusive) and 100 (inclusive)"
I want to increase per page limit. I already have done with products by using below code and want to do the same with categories
function maximum_api_filter($query_params) {
$query_params['per_page']["maximum"]=100000;
return $query_params;
}
add_filter('rest_product_collection_params', 'maximum_api_filter');
I must've commented very early in the morning
There is NO Way of increasing the size per page past a 100 Its a limit set by the API team you cannot exceed a 100.
Now I do not know what Your using to develop, But i'd consider using a Collection and just pull the first 100, Index the Page pull a 100 Combine Collections and so on.
But as for Getting all 300 in one instance from one API call that cannot be done sadly.
I also face the same problem.
You couldn't increase the list size. Maximum limit in Woocommerce is 100.
You can follow bellow URL pattern to fetch 100 records,
https://www.yourcompany.com/wp-json/wc/v2/products?per_page=100
You can also specify pages with the ?page parameter
https://www.yourcompany.com/wp-json/wc/v2/products?per_page=100&page=2
You can find total number of pages and records from
the X-WP-TotalPages and X-WP-Total HTTP headers.
After getting total page from X-WP-TotalPages you can execute a loop with per_page=100&page=${pageNumber} and increase pageNumber dynamically.
By this approach you can find all records.

WordPress Rest API get total post count

I am using the following to get posts
http://demo.wp-api.org/wp-json/wp/v2/posts
I the max limit on posts is 100 per request.
So I would have to make multiple requests to get all posts.
How can I get the total post count so I know how many requests I have to make?
Thanks.
The response headers include this data:
To determine how many pages of data are available, the API returns two
header fields with every paginated response:
X-WP-Total: the total number of records in the collection
X-WP-TotalPages: the total number of pages encompassing all available
records
By inspecting these header fields you can determine how much more data is available within the API.
via https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/
also for reference: https://wordpress.stackexchange.com/a/250589
Try this instead for pagination. It returns all the posts on my site.
http://example.com/wp-json/wp/v2/posts/?filter[posts_per_page]=10
filter[category_name]
http://example.com/wp-json/wp/v2/posts/?filter[category_name]=country&filter[posts_per_page]=111

Different results with and without ga:pagePath dimension

I use the following query parameters in Google Analytics:
start-date=2017-10-01
end-date=2017-10-01
metrics=ga:users
When I filter by ga:pagePath==/charity/ the result is 100.
When I filter by ga:pagePath=~/charity/ the result is 139.
When I filter by ga:pagePath=~/charity/;ga:pagePath!=/charity/ The result for some reason is 71. How is it possible?
Moreover, if I add the ga:pagePath dimension when I filter by ga:pagePath=~/charity/ I can see that the results add up to way more than 139. In fact, it shows that users for /charity is 100 and the rest of URLs add up to 137 (counted them manually). Could someone, please, help me to understand what am I missing here?
I wager that at least part of the problem is that you query a user scope metric and filter by a hit based dimension.
A user can see multiple pages that contain "charity" in the url, so the total from all pages is more than total users, e.g, if one user has seen page /charity/a and page charity/b the rows would add to two users, but the absolute total is just the single person.

Pagination in application which use firebase.com as database

Front end application will use firebase.com as database.
Application should work like blog:
Admin add / remove posts.
There are "home" page which show several articles per page with "Next" and "Prev" buttons and page which show single article.
Also I need deep linking.
I.e. when user enter URL http://mysite.com/page/2/ I need that application show last articles from 10 to 20, when user enter URL http://mysite.com/page/20/ application show last articles from 200 to 210. Usual pagination.
Main question - is it possible to achieve this if use firebase.com.
I read docs at firebase.com, I read posts here. "offset()" and "count()" will be in the future, use priority, in order to know count of items and not load all of them use additional counter.
Based on this I suppose this:
ADD POST action:
get value of posts counter
set priority for new post data equals to value of posts counter
increase value of posts counter
DELETE POST action
get value of priority for post data
query posts data which have value of priority more than priority for post data which will be deleted and decrease their value
decrease value for posts counter
GET POSTS FROM x TO y
postsRef.startAt(null, x).endAt(null, y).on(... and so on)
Thing which I not like in this way are actions for DELETE POST. Because index of posts will be from 0 to infinity and post with less priority is considered as post which was created earlier so if I have 100000000 posts and if I need to delete 1st post then I need to load data for 99999999 next posts and update their priority (index).
Is there any another way?
Thanks in advance, Konstantin
As you mentioned, offset() will be coming, which makes your job easier.
In the meantime, rather than using startAt and endAt in combination, you could use startAt and limit in combination to ensure you always get N results. That way, deleted items will be skipped. You then need to check the last id of each page before moving to the next to find the proper id to start on.

Resources