1

I'm trying to build a custom search results page. The problem I'm running into is that when I enter a search term ; say 'gaming', it will only return one result, being the latest result. I have my blog setup to display only one article at a time. However, I found a way to circumvent the search results page by logging into wp-admin -> settings -> reading -> and changing the blog page displays one post to say for example 10.

Then when I try searching for gaming it returns more than one result (it'll give me 10). However, I don't want this. I want my sites blog page to only show one article, and the results page to display more than one result. How can I go about fixing this?

This is what my search.php looks like. I created a searchpage.php and am including it using PHP to grab the search.php

Thanks! :)

    <!-- SEARCH PAGE -->
    <div id="search-results" class="wrapper" role="search">

<h2> Search Results</h2>

<!-- COUNT RESULTS -->
<div class="results">
    <?php
    /* Search Count */
    $allsearch = &new WP_Query("s=$s&showposts=-1"); 
    $key = wp_specialchars($s, 1);
    $count = $allsearch->post_count; 
    _e('');
    _e('"<span class="search-terms">');
    echo $key;
    _e('</span>"'); 
    _e(' &mdash; found ');
    echo $count . ' '; 
    _e('articles');
    wp_reset_query(); ?>

</div>
<!-- / COUNT RESULTS -->

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

<!-- LIST RESULTS -->
<section>   
    <h3>
        <li>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to 
        <?php the_title_attribute(); ?>"><?php the_title(); ?></a> - 
        <span class="search-time"><?php the_time('F, j, Y') ?></span>
        </li>
    </h3>
</section>
<!-- / LIST RESULTS -->

<?php endwhile; else: ?>

<!-- 404 SEARCH -->
<div class="404-search">
<?php _e("Oops... We couldn't find what you were searching for. Please try again"); ?>
</div>
<!-- / 404 SEARCH -->

<?php endif; ?>

     </div>

     <!-- / SEARCH PAGE -->

1 Answer 1

2

Change

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

to

<?php if ($allsearch->have_posts()) : ?>
<?php while ($allsearch->have_posts()) : $allsearch->the_post(); ?>

because otherwise you don't utilize your custom query.

See WP_Query Usage.

Also call wp_reset_query(); after the Loop.

See wp_reset_query Description.

Actually, you don't need wp_reset_query(); because you use custom WP_Query object and don't need to reset global $wp_query.

1
  • Awesome that did the job! Thanks! I tried look all over Google and I couldn't find anything related to this. Thanks a bunch! :3
    – Monstr92
    Commented Mar 16, 2013 at 12:33

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.