iWizard
iWizard

Reputation: 7104

How to get number of results from query_posts?

I'm printing posts and I want to get number of results, how can I do that?

This is part of my code:

if (have_posts()) : 

    $args = array(
        'showposts' => '5',
        'paged' => $paged
    );


    $thePosts = query_posts($args);
...

Thank's for help

Upvotes: 20

Views: 82382

Answers (7)

Giang D.MAI
Giang D.MAI

Reputation: 244

query_posts( $args );
global $wp_query;
print_r($wp_query->max_num_pages);

It help me.

Upvotes: 0

George
George

Reputation: 6084

This will give you the results: Showing results 11-20 of 46, for instance.

  $args = array(
    'cat'=> $cat,
    'posts_per_page' => 10,
    'paged' => $paged,
    's'=> $s
  );
  query_posts($args);

  $startpost=1;
  $startpost=10*($paged - 1)+1;
  $endpost = (10*$paged < $wp_query->found_posts ? 10*$paged : $wp_query->found_posts);
        ?>
  <h2 class="displayResult">Showing results <?php echo $startpost; ?> - <?php echo $endpost; ?> of <?php echo $wp_query->found_posts; ?></h2>

If this is not a search page, simply remove the line "'s'=> $s".

If you do need it, make sure you declare the variable as $_GET['s'] above.

Upvotes: 6

kdgilang
kdgilang

Reputation: 571

display numbers of search results :

<?php global $wp_query;
echo $wp_query->post_count; ?> 

Upvotes: 2

kaiser
kaiser

Reputation: 22353

Easy. To display number of results for this current page, use

// Showing Page X of Y
print filter_var( absint( $GLOBALS['wp_query']->post_count ), FILTER_SANITIZE_NUMBER_INT );

For the total amount of results, use

print filter_var( absint( $GLOBALS['wp_query']->found_posts ), FILTER_SANITIZE_NUMBER_INT );

Upvotes: 2

Gabriel Reguly
Gabriel Reguly

Reputation: 350

The correct answer is

 if (have_posts()) : 

    $args = array(
        'showposts' => '5',
        'paged' => $paged
    );


    $thePosts = query_posts($args);



     echo $thePosts ->found_posts;
...

Upvotes: 7

iWizard
iWizard

Reputation: 7104

SOLVED:

if (have_posts()) : 

        $args = array(
            'showposts' => '5',
            'paged' => $paged
        );


        $thePosts = query_posts($args);


         global $wp_query; 
         echo $wp_query->found_posts;
    ...

Upvotes: 50

Ryan B
Ryan B

Reputation: 3392

To display the number of results of a search, use:

Search Result for 

<?php 
/* Search Count */ 
$allsearch = &new WP_Query("s=$s&showposts=-1"); 
$key = wp_specialchars($s, 1);
$count = $allsearch->post_count; _e('');
 _e('<span class="search-terms">'); 
echo $key; _e('</span>'); 
_e(' &mdash; '); 
echo $count . ' ';
 _e('articles');
 wp_reset_query(); 
?>

This was taken from: WP Beginner.

Upvotes: 7

Related Questions