21

I have a wp_query $wp_query = new WP_Query($args); but for some reason the $wp_query->post_count only shows the amount of posts on each page. So if I set 'showposts' => '10' and 12 results are found, page one will say '10 results found' and page 2 shows '2 results found'. Why is this? I can't figure it out.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
  'post_type' => 'post', 
  'showposts' => '10',
  'paged' => $paged
);
$wp_query = new WP_Query($args);
echo "Search found ".$wp_query->post_count."results";
if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();
// the loop
1
  • 2
    Have you looked at the variable $wp_query->found_posts as well?
    – fuxia
    Commented Dec 4, 2012 at 13:52

1 Answer 1

42

$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

2
  • 2
    ..and as simple as that, it works. Thanks so much guys!
    – zilj
    Commented Dec 4, 2012 at 14:15
  • I have a problem with this. When I filter results using found_posts - the number changes. I want to keep the static number of total results intact. How could I do this ?
    – JoaMika
    Commented May 12, 2017 at 16:13

Your Answer

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

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