0

I have a following search.php page (this is just the code required for problem presentation).

Why is my $total_results returning a number of posts found, but when I do a loop though it always print No posts found ?

<?php

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query_args = explode("&", $query_string);
$search_query = array('posts_per_page' => 10, 'paged' => $paged);

foreach($query_args as $key => $string) {
     $query_split = explode("=", $string);
     $search_query[$query_split[0]] = urldecode($query_split[1]);
} 
$search = new WP_Query($search_query);
$total_results = $search->found_posts; // this returns 50

?>


<?php if ($search->have_posts()) : ?>
<?php while ($search->have_posts()) : $search->the_post(); ?>
    loop  through posts
<?php else : ?>
     No posts found.
<?php endif; ?>

Any suggestion much appreciated.

6
  • The only reason i can think of is that you're on page 6 in the query. Try doing var_dump($search->posts); & see if that's as expected Commented Jan 28, 2013 at 10:49
  • The number of result is 50 for given keyword, if I change the keywords I am search for the number of result is different.
    – Iladarsda
    Commented Jan 28, 2013 at 11:14
  • @MridulAggarwal - var_dump($search->posts); comes back as array(0) { }
    – Iladarsda
    Commented Jan 28, 2013 at 11:16
  • array(0) { } means no data is coming from the database. Check your paged parameter. 50 posts with 10 per page means the paged parameter can be 5 at max Commented Jan 28, 2013 at 12:01
  • try var_dump( $search_query );
    – Michael
    Commented Jan 28, 2013 at 15:08

1 Answer 1

1

WP_Query::found_posts contains the number of matches to your query without taking into account limit and offset. limit is the max number of posts which can be display in an archive page and the offset depends on the paged parameter and is usually ($paged - 1) * $limit.

So what you got is the number of results you would have gotten for your search query if you asked to get all the posts that match the search criteria.

If you need to know the number of the results for the specific query including the limit and offset you can simply do $search->post_count.

5
  • Don't use count($search->posts). You can read the same from $search->post_count Commented Jan 28, 2013 at 12:23
  • edited as suggested Commented Jan 28, 2013 at 12:32
  • $search->post_count comes back with 0. What is wrong with my query? I am searching for a tag name - tag has been used in couple of post, which I can see under admin/post/tag . I also added the Search Everywhere plugin
    – Iladarsda
    Commented Jan 28, 2013 at 13:46
  • 2
    tag names are not part of normal search. search is performed in the title and content. I actually don't understand the question anymore, is it about how the search is performed or how to know how many results are there? Commented Jan 28, 2013 at 16:01
  • @MarkKaplun - the SQL server (DB Abstraction) was to be fault. The code was OK - meaning my question was wrong as I was expecting the code to be at fault.
    – Iladarsda
    Commented Jan 29, 2013 at 11:05

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.