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:

<?php
/**
 * The template for displaying Search Results pages.
 *
 * @package League Of Legends: Rift
 */
get_header(); 
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>
        </header>
    <?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(); ?>
                <?php
                /**
                 * 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 -->
<?php 
if($sidebar_active){
get_sidebar();
} 
get_footer();
?>

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

Read more here: Search page results conflicting logic - Search result caching?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, , , , , ,

Wordpress related questions and answers: