WordPress.org

Ready to get started?Download WordPress

Forums

Search results only displaying one result even though there are many. (2 posts)

  1. schellbpe
    Member
    Posted 1 year ago #

    I have a search query to search my entire site for something. When I test it, it says "There were 5 results" but only one link or post shows up. I have tested the while loop and it appears as though the problem lies somewhere in the query itself. Its like it is not pulling the results?? Here is the code below, your help is appreciated

    <?php
    /*
    Template Name: Search Page
    */
    ?>
    
    <?php
    global $query_string;
    
    $query_args = explode("&", $query_string);
    $search_query = array();
    
    foreach($query_args as $key => $string) {
    	$query_split = explode("=", $string);
    	$search_query[$query_split[0]] = urldecode($query_split[1]);
    } // foreach
    
    $search = new WP_Query($search_query);
    ?>
    
    <?php
    global $wp_query;
    $total_results = $wp_query->found_posts;
    ?>
    
    <?php get_header(); ?>
    
    <div class="row">
      <div class="span8">
    	<?php get_search_form(); ?>
    
    	<h1>Search Results</h1>
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<h2 class="post-title">
    	<a>" ><?php the_title(); ?></a>
    	</h2>
    <?php endwhile; else: ?>
    <div class="entry"><h2><?php _e('Sorry, no posts matched your Search criteria.'); ?></h2></div>
    <?php endif; ?>
    	There were: <strong><?php echo $total_results; ?></strong> search results.
      </div>
      <div class="span4 post-list">
    		<?php get_sidebar(); ?>
      </div>
    </div>
    
    <?php get_footer(); ?>

    [Please post code or markup between backticks or use the code button. Or better still - use a pastebin. Your posted code may now have been permanently damaged/corrupted by the forum's parser.]

  2. schellbpe
    Member
    Posted 1 year ago #

    Oops...this may be better

    <?php
    /*
    Template Name: Search Page
    */
    ?>
    
    <?php
    global $query_string;
    
    $query_args = explode("&", $query_string);
    $search_query = array();
    
    foreach($query_args as $key => $string) {
    	$query_split = explode("=", $string);
    	$search_query[$query_split[0]] = urldecode($query_split[1]);
    } // foreach
    
    $search = new WP_Query($search_query);
    ?>
    
    <?php
    global $wp_query;
    $total_results = $wp_query->found_posts;
    ?>
    
    <?php get_header(); ?>
    
    <div class="row">
      <div class="span8">
    	<?php get_search_form(); ?>
    
    	<h1>Search Results</h1>
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<h2 class="post-title">
    	<a href="<?php the_permalink()?>" ><?php the_title(); ?></a>
    	</h2>
    <?php endwhile; else: ?>
    <div class="entry"><h2><?php _e('Sorry, no posts matched your Search criteria.'); ?></h2></div>
    <?php endif; ?>
    	There were: <strong><?php echo $total_results; ?></strong> search results.
      </div>
      <div class="span4 post-list">
    		<?php get_sidebar(); ?>
      </div>
    </div>
    
    <?php get_footer(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic