Empty search returns home page, how to return not found search page?

The default search function if the search form is empty returns the home page, I want it to return a “sorry your search returned no results” page.

this post doesn’t answer it

and this ticket tells me that it is supposed to function that way! Anyone figure out how to change it besides using a .htaccess redirect?

I’m using the following search.php file:
`

        <div id="content" class="clearfix">

            <div id="main" class="col700 left clearfix" role="main">

                <h1 class="archive_title"><span>Search Results for:</span> <?php echo esc_attr(get_search_query()); ?></h1>

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

                <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?>>

                    <header>

                        <h3><a href="https://wordpress.stackexchange.com/questions/29516/<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

                        <p class="meta"><?php _e("Posted", "bonestheme"); ?> <time datetime="<?php echo the_time('Y-m-j'); ?>" pubdate><?php the_time('F jS, Y'); ?></time> <?php _e("by", "bonestheme"); ?> <?php the_author_posts_link(); ?> <span class="amp">&</span> <?php _e("filed under", "bonestheme"); ?> <?php the_category(', '); ?>.</p>

                    </header> <!-- end article header -->

                    <section class="post_content">
                        <?php the_excerpt('<span class="read-more">Read more on "'.the_title('', '', false).'" &raquo;</span>'); ?>

                    </section> <!-- end article section -->

                    <footer>


                    </footer> <!-- end article footer -->

                </article> <!-- end article -->

                <?php endwhile; ?>  

                <?php if (function_exists('page_navi')) { // if expirimental feature is active ?>

                    <?php page_navi(); // use the page navi function ?>

                <?php } else { // if it is disabled, display regular wp prev & next links ?>
                    <nav class="wp-prev-next">
                        <ul class="clearfix">
                            <li class="prev-link"><?php next_posts_link(_e('&laquo; Older Entries', "bonestheme")) ?></li>
                            <li class="next-link"><?php previous_posts_link(_e('Newer Entries &raquo;', "bonestheme")) ?></li>
                        </ul>
                    </nav>
                <?php } ?>          

                <?php else : ?>

                <!-- this area shows up if there are no results -->

                <article id="post-not-found">
                    <header>
                        <h1>No Results Found</h1>
                    </header>
                    <section class="post_content">
                        <p>Sorry, but the requested resource was not found on this site.</p>
                    </section>
                    <footer>
                    </footer>
                </article>

                <?php endif; ?>

            </div> <!-- end #main -->

            <div id="sidebar1" class="sidebar right col220">

                <?php get_search_form(); ?>



            </div>

        </div> <!-- end #content -->

`

8

Here are 3 ways of fixing this, I advise you use solution 2, but pay attention to the jQuery in solution 1 as a way of avoiding the situation in the first place.

For those wanting more code posted from the question askers theme, this is not a theme issue, this is a general WordPress issue that affects all WordPress sites.

Solution 1

You can find an in depth tutorial on how to fix this here:

Fix Empty Searches

Today, let’s look at something, that most professionals never see:
empty searches. You offer a search input field, and someone hits the
submit button unintentionally, without any term entered. The resulting
URI looks like this: example.com/?s=. It shows the same content as
your front page. In fact, it is the front page.

No one needs that.

Solution 2 (recommended)

Taken froma post by Spitzerg http://wordpress.org/support/topic/blank-search-sends-you-to-the-homepage

Another option is to add a request filter:

add_filter( 'request', 'my_request_filter' );
function my_request_filter( $query_vars ) {
    if( isset( $_GET['s'] ) && empty( $_GET['s'] ) ) {
        $query_vars['s'] = " ";
    }
    return $query_vars;
}

Then if you’re reusing the search query in your search form don’t
forget to trim it so you don’t end up with one or more spaces (just to
keep things clean, probably won’t affect results.

<input type="text" name="s" id="s" value="<?php echo trim( get_search_query() ); ?>"/>

Hope this helps, it seems to be working thus far on my site and
doesn’t involve changing any of the WP code making upgrades easier.

Solution 3

Fix Redirection and Error Page On Empty WordPress Search

Similar to solution 2 but not as extensive and slightly different.

    if(!is_admin()){
        add_action('init', 'search_query_fix');
    }
    function search_query_fix(){
        if(isset($_GET['s']) && $_GET['s']==''){
            $_GET['s']=' ';
        }
    }

Leave a Comment