Forums

Weird Search Count Issue... (4 posts)

  1. revlimiter
    Member
    Posted 3 years ago #

    I am trying to figure out why my search results are sometimes showing the incorrect number when doing a search.
    I think it has something to do with WordPress searching searching for *both* posts and pages, but only showing posts.

    http://www.kimstewart.ca/?s=test&submit.x=0&submit.y=0&submit=Search
    From the link above you can see there are 3 Search Results for "test", but only 2 show up...

    Here is my code in search.php

    <?php include_once($_SERVER['DOCUMENT_ROOT'] . "/inc/php/header.php");
    $postnum = 1;
    ?>
    
    <div id="content">
    	<div id="content_box">
    
    		<div id="content" class="posts">
            <?php
    //Initialize $pagenum
    $pagenum = $paged;
    
    //Set 1st page to 1 instead of 0
    if($pagenum == ''){$pagenum = 1;}
    
    //Start checking which page is up
    if ($pagenum == 1) {
    
    //On 1st page start counting posts from 1
    $postnum = 1;
    } else {
    
    //On following pages start counting from 11, 21, 31, etc.
    $postnum = ($pagenum * 10) - 9;
    }
    ?>
    
    		<?php if (have_posts()) : ?>
    
    			<h2 class="archive_head"><?php wp_searchheader()?> Search Results for <em class="grey">"<?php the_search_query(); ?>"</em></h2>
    
    			<?php while (have_posts()) : the_post(); ?>
    <?php if (is_type_page()) continue; ?>
    			<div class="postnum"><?php echo $postnum; ?>.</div>
    			<h2 class="padding"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    			<h4 class="padding"><?php the_time('F jS, Y') ?><!-- by <?php the_author() ?> --></h4>
    			<div class="entry">    
    
    <?php // if there's a img in my custom field
    if($value !== '') { ?>
    <img src="<?php $values = get_post_custom_values("thumbnail"); echo $values[0]; ?>" class="results_thumb" />
    <?php } // end if statement
    // if there's no img
    else { echo 'link of img'; } ?>
    
    <?php the_excerpt() ?>
    			</div>
    			<p class="tagged"><?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?> <strong class="tag">Tags:</strong> <?php the_category(' &middot; ') ?>
    
    		<?php $postnum++; ?>
    			<?php endwhile; ?>
    			<?php if ($postnum>9) { ?>
    			<?php include (TEMPLATEPATH . '/navigation.php'); ?>
    		<?php } ?>
    		<?php else : ?>
    
    			<h2 class="page_header">No Search Results for <em class="grey">"<?php the_search_query(); ?>"</em></h2>
    			<div class="entry">
    				<!--<?php include (TEMPLATEPATH . '/searchform.php'); ?>-->
    			</div>
    		<?php endif; ?>
    
    		</div>
    
    		<?php get_sidebar(); ?>
    
    	</div>
    </div>
    
    <?php include_once($_SERVER['DOCUMENT_ROOT'] . "/inc/php/footer.php"); ?>
  2. revlimiter
    Member
    Posted 3 years ago #

    bump

  3. whooami
    Member
    Posted 3 years ago #

    youre not providing the one impiortant pice of info.

    this:

    wp_searchheader

    is providing that number. WHAT IS THAT.

    there's a few versions of that search results plugin floating around, i think theres even one on my own site. It would be very useful obviously, if you provided the link to the one you happen to be using.

  4. revlimiter
    Member
    Posted 3 years ago #

    Gey whooami,
    Thanks for pointing that out! :-)

    Here is the code for the plugin "Results Count":

    <?php
    /*
    Plugin Name: Results count
    Plugin URI: http://wordpress.org/extend/plugins/results-count/
    Description: Adds info to search results for how many matches were found
    Version: 0.3
    Author: Matthew Taylor
    Author URI: http://www.mtaylor.co.uk
    */
    
    ### Search results count heading
    function wp_searchheader() {
    
    		 if (is_search() || is_date() || is_category() || is_tag()) {
    			global $request, $posts_per_page, $wpdb, $paged, $wp_query;
    
    				preg_match('#FROM (.*) GROUP BY#', $request, $matches);
    				$fromwhere = $matches[1];
    
    				$numposts = $wp_query->found_posts;
    
    				if (0 < $numposts) $numposts = number_format($numposts);
    
    				if(empty($paged)) {
    					$paged = 1;
    				}
    
    				$startpost = ($posts_per_page * $paged) - $posts_per_page + 1;
    
    				if (($startpost + $posts_per_page - 1) >= $numposts) {
    					$endpost = $numposts;
    				} else {
    					$endpost = $startpost + $posts_per_page -1;
    				}
    
    				$search_terms = get_query_var('search_terms');
    
    				if ($posts_per_page!=-1) {
    					echo '<!--<p>Showing results <b> '.$startpost. '</b> - <b>' .$endpost. '</b> of <b>-->' .$numposts. '<!--</b> for -->';
    				} else {
    					echo '<p>Showing <b>'.$numposts.'</b> results for ';
    				}
    
    				if (is_search()){
    					echo '<!--the search terms: <b>-->';
    
    					for ($i = 0; $i < count($search_terms); $i++){
    						echo '<!-- <a href="index.php?s=', $search_terms[$i], '" title="search this site for: ', $search_terms[$i], '">', $search_terms[$i], '</a>-->';
    					}
    				}
    
    				if (is_date()){
    					$wholedate = (trim(wp_title('', FALSE)));
    					$dateArray = (explode(" ", $wholedate));
    					$date = ($dateArray[2] . ", " . $dateArray[0]);
    					echo '<b>', $date;
    				}
    
    				if (is_category()){
    					echo 'the category <b><a href="http://', $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"], '">', single_cat_title(),'</a>';
    				}
    
    				if (is_tag()){
    					echo 'the tag <b><a href="http://', $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"], '">', single_tag_title(),'</a>';
    				}
    
    			echo '<!--</b>.</p>-->';
    
    		}
    	}
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.