1

I have a custom search results page that works fine except when an empty input box is submitted.

When an empty input box is submitted, the header on the results page displays "XX results found" (the total number of pages/posts on the website) but no results are listed on the page.

So it seems like the code displaying the header count is finding every page on my site, while the actual code displaying the results isn't finding anything. I don't care which behaviour is used for these empty searches (display everything vs. display nothing), but I'd prefer that the behaviour is consistent between the count in the header and the actual results.

This is the code for the header (that returns "XX results found"):

<h2>Search results for 
<?php 
  $allsearch = new WP_Query("s=$s&showposts=-1");
  $key = esc_html($s, 1);
  $count = $allsearch->post_count; 
  _e(''); _e('&#8220;'); _e('<span class="searchTerm">');
  echo $key; 
  _e('</span>'); _e('&#8221;'); _e(' - ');
  echo $count . ' '; _e('found');
  wp_reset_query(); 
?>
</h2>

And this is the code for the actual results (that returns nothing):

<div id="searchContent">
<?php
  global $query_string;
  query_posts( $query_string . '&posts_per_page=20' );
  while(have_posts()) : the_post(); ?>
    <p>
      <a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
      <span class="searchDate"><?php the_time('j F Y'); ?></span>
    </p>
<?php endwhile; ?>
</div> <!-- search content -->
  • Hi! Might be worth making sure to paste the rest of the second bit of code just for completeness, and/or OCD of programmers who like closed tags ;-) – mozboz Jun 28 at 18:42
  • Thanks for the suggestion. I will do that! – mroulston Jun 28 at 18:44
0

I'm not overly familiar with search results pages and how you edit them, but a quick fix for the problem you have is to check for an empty string in the first bit of code and say "Please enter a search term" or something if there isn't one entered. You'd achieve that with something like:

<?php
  if (empty($s)) {
     _e('Please enter a search term');
  } else {
    $allsearch = new WP_Query("s=$s&showposts=-1");
    $key = esc_html($s, 1);
    $count = $allsearch->post_count; 
    _e(''); _e('&#8220;'); _e('<span class="searchTerm">');
    echo $key; 
    _e('</span>'); _e('&#8221;'); _e(' - ');
    echo $count . ' '; _e('found');
    wp_reset_query(); 
  }
?>

Extra stuff not relevant to your question: For the second bit of code it may be worth looking at the docs here https://developer.wordpress.org/reference/functions/query_posts/ and switching to using get_posts, but as I said I'm not sure if using query_posts might be ok in this case for search pages. Certainly the docs say it shouldn't be used in your own themes.

| improve this answer | |
  • Thanks very much! I tried your code and it works great. Even if I can't figure out how to get the second bit code working better, the fix you gave me for the first bit of code is a fantastic workaround. I really appreciate you taking the time to help me out. – mroulston Jun 28 at 19:03
  • @mroulston welcome :) upvoting and accepting the answer would be great if it worked for you :) – mozboz Jun 28 at 19:24
  • 1
    Done and done. Looks like my upvote doesn't show publicly because I'm new, but I did upvote it as well as marking it as accepted. Thanks for the reminder. Still figuring out how everything works here. – mroulston Jun 28 at 19:28

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.