I'm doing the pagination for a web. < Previous (pagenumber) Next > is easy, already done. But now I need to add a selector to go to a page directly (ex: to page 7), to do so, I need to know how many pages there are, and for this I need to count how many posts were found in the query.
The problem is that this web has too many posts (> 13.000) and querying all of them slows down the page loading, and it takes like... 10 seconds for the page to load. Obviously this is not acceptable. Pagination solves that problem because only 50 or 100 posts are loaded at a time, but then I can't count them all.
Can I count posts in a certain query without loading them? Or can I get the number of pages any other way?
I can't include ALL the code, because it's too large, but basically, it's a shortcode and I do this:
$args = array();
$args['parameters...'] = value;
$args['posts_per_page'] = 50;
$args['offset'] = 50 * page; <-- pagination offset
...
$posts = get_posts( $args ); <-- I want to count this without posts_per_page = 50
I tried doing: $count = sizeof($posts);
And this works, but it takes too long because, additionally, I have to run the query twice: one with posts_per_page = -1 and one with posts_per_page = 50.