I recently stylized my search page results from the base template _S, and got everything to work quite nicely. I've developed it to return false/positive header/body. It should only return a positive header if there are actual results found, and then the positive body should contain those results, otherwise, it should return the false versions of the two of them. However, with some queries (which should be getting search results) somehow return a "positive" header, and for whatever reason, a "negative" body (telling the user that there are no search results). Strangely enough, I've tested other words, and those words act as they should, with the proper false/positive responses being returned. You can check it out here.

On my site, you can try searching for something like "Shortcodes", and will return up with a positive header which says something along the lines of "Search Results for: Shortcodes" and will then list the results that it found, whereas if you search for something like "Search", it will return that positive header for there being posts there, but it won't render out any posts, and instead render out the negative body, which tells you that there are no search results. (Even though I have made a post about "searching" recently... which is perplexing.

My search.php is:

 * The template for displaying Search Results pages.
 * @package League Of Legends: Rift


if ( have_posts() && $pco_rift['opt-search-sidebar']['2'] || !have_posts() && $pco_rift['opt-search-sidebar']['1']){
    $sidebar_active = true;
    <?php if ( have_posts() ) : ?>
        <?php if ( have_posts() ) : the_post(); ?>
                <header class="page-header search-header search-success">
                        <h1 class="page-title"><?php printf( __( '<i class="fa fa-search"></i> Search Results for: %s', 'league-of-legends-rift' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
                </header><!-- .page-header -->
        <?php endif; ?>
    <?php else : ?>
        <header class="page-header search-header search-fail">
                <h1 class="page-title"><?php _e('Nothing Found', 'league-of-legends-rift')?></h1>
    <?php endif; ?>

    <section id="primary" class="content-area">
        <main id="main" class="site-main" <?php if(!$sidebar_active){ echo 'style="margin: 0" '; } ?>role="main">

        <?php if ( have_posts() ) : ?>

            <?php /* Start the Loop */ ?>
            <?php while ( have_posts() ) : the_post(); ?>

                 * Run the loop for the search to output the results.
                 * If you want to overload this in a child theme then include a file
                 * called content-search.php and that will be used instead.
                get_template_part( 'content', 'search' );
            <?php endwhile; ?>
            <?php league_of_legends_rift_paging_nav(); ?>
        <?php else : ?>
            <h4><?php _e( 'Sorry, but nothing matched your search terms. Please try again with some different keywords.', 'league-of-legends-rift' ); ?></h4>
            <?php get_search_form(); ?>
        <?php endif; ?>

        </main><!-- #main -->
    </section><!-- #primary -->

I've setup a sidebar active thing at the top, and what it does is it will not render the sidebar if there are no search results (because this code will render out a nice long search bar, which I don't want to cause the duplication of the one in the sidebar).

Interestingly enough, my other random queries like "gaspdiojfapsoidfjasdf" will return everything correctly (negative header, negative body).

Thanks, Adam

1 Answer 1


Hm, I'm not sure, but your use of have_posts may be problematic.

Note the codex states "As a side effect, have_posts starts, steps through, or resets The Loop.".

High up in your code you have

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

This may be causing unexpected results by messing with the loop.


I think you're getting 'correct' results for 'Shortcodes' (in your example) because 'Shortcodes' gets multiple hits. 'Search', however, has only one hit, and that hit is being looped over in your header, after which the loop is spent.

So, you need to reset the loop after running have_posts(), with rewind_posts().

  • Hmm, well I added that in after the fact of noticing the error, hoping it would fix it. At any rate, the following pastebin.com/kbknqkvt (that's the paste of my code with the suggested problem) won't work. I really have no idea what's wrong with this.
    – Adam
    Jun 27, 2014 at 18:05
  • I've edited my answer...
    – vancoder
    Jun 27, 2014 at 18:11
  • Thank you! :D I did not realise the manner in which the looped worked! (I was just basing my work off deduction from what was already included in the theme)
    – Adam
    Jun 27, 2014 at 18:20

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.