0

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.

4
  • Have you looked at the various pagination functions WordPress provides already?
    – swissspidy
    Jan 19, 2018 at 11:49
  • Yes but it's a specific plugin I'm writing. I'll look into it, but I haven't found anything useful.
    – Sertilou
    Jan 19, 2018 at 11:50
  • They can still be useful in that context. Can you share the code you‘re using for your query then?
    – swissspidy
    Jan 19, 2018 at 11:51
  • I updated the question
    – Sertilou
    Jan 19, 2018 at 12:05

1 Answer 1

1

$wp_query->post_count is supposed to work exactly like that. To get the total number of posts that exist in the database, use $wp_query->found_posts

6
  • mmm yeah I'm not using WP_Query, but get_posts($args), which returns an array and there's where I spend loading time. I guess I need to change my system. Can I use foreach with WP_Query instead of the while loop?
    – Sertilou
    Jan 19, 2018 at 12:09
  • Furthermore, I tried it and it has the same problem: my page takes too long to load. I need to count WITHOUT running the query. Like a SQL COUNT( ). What this does is like SELECT * and then counting the size of the array.
    – Sertilou
    Jan 19, 2018 at 12:23
  • get_posts() is just a wrapper for WP_Query… So you are using it. Why do you need a foreach()? You can look at wp_count_posts() for inspiration on how to count the posts, but if I were you I'd just check $wp_query->max_num_pages and use paginate_links() or get_the_posts_pagination(). Also check out wordpress.stackexchange.com/questions/174907/…
    – swissspidy
    Jan 19, 2018 at 13:06
  • @swissspidy While get_posts() does use WP_Query, it doesn't overwrite the global $wp_query variable so you wouldn't be able to use any of the $wp_query properties such as max_num_pages or post_count.
    – Howdy_McGee
    Jan 24, 2018 at 17:55
  • 1
    @Howdy_McGee My point was that you are able to use these properties when they'd be using $my_query = new WP_Query(). Thus, get_posts() isn't really the most helpful function.
    – swissspidy
    Jan 24, 2018 at 18:00

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.