Search results not showing the correct number (3 posts)

  1. timtastic
    Posted 9 years ago #

    I'm having a problem with displaying the number of my search results:


    If you visit the link, you'll see "2 results found", when actually there are more than 2. You can navigate to the previous entries and see more results.

    It seems to do this whenever more than 2 results are found. Can anyone fix my search.php code?

    <?php get_header(); ?>
    <div id="primary" class="single-post">
      <div class="inside">
        <div class="primary">
          <?php if (have_posts()) : ?>
          <h1>Search Results</h1>
          <ul class="dates">
            <?php while (have_posts()) : the_post(); ?>
            <li> <span class="date">
              <?php the_time('n.j.y') ?>
              </span> <a href="<?php the_permalink() ?>">
              <?php the_title(); ?>
              </a> |
              <?php the_category(', ') ?>
    <p />
    <?php the_excerpt_reloaded(50, 'all', 'content', FALSE); ?>
            <?php $results++; ?>
            <?php endwhile; ?>
          <div class="navigation">
            <div class="left">
              <?php next_posts_link('&laquo; Previous Entries') ?>
            <div class="right">
              <?php previous_posts_link('Next Entries &raquo;') ?>
          <?php else : ?>
          <h1>No posts found. Try a different search?</h1>
          <?php endif; ?>
        <div class="secondary">
          <div class="featured">
            <p>You searched for &ldquo;<?php echo wp_specialchars($s, 1); ?>&rdquo;
              <?php bloginfo('name'); ?>
              . There were
    				if (!$results) echo "no results, better luck next time.";
    				elseif (1 == $results) echo "one result found. It must be your lucky day.";
    				else echo $results . " results found.";
        <div class="clear"></div>
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
  2. topdownjimmy
    Posted 8 years ago #

    The crucial line here is <?php $results++; ?>

    This just increments a PHP variable for every post displayed on the current page (which you apparently have set to a limit of 2). It can't count the posts on other pages.

    I think in order to display the number of search results accurately, you need to install a plugin.

  3. whooami
    Posted 8 years ago #

    dont need a plugin to do that, just need a little php magic:

    http://www.village-idiot.org/search/jkhkjlhkjhkhkh (0)

    http://www.village-idiot.org/search/christmas (lots)

    http://www.village-idiot.org/search/rootlab (just one)

    check the titles of the pages.

    it's magic, I tell ya!

Topic Closed

This topic has been closed to new replies.

About this Topic