Change loop.php for empty search customization

I’ve been stumped for days wondering why when I submit an empty string as a search term, it redirects to part of loop.php where I have the following code inserted:

<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( !(have_posts()) && !(is_search()) ) : ?>
    <h1 class="notfound-header"><?php _e( 'Not Found', 'twentyten' ); ?></h1>
    <p class="notfound-content"><?php _e( 'Apologies, but no results were found.', 'twentyten' ); ?></p>
<?php elseif ( !(have_posts()) && (is_search()) ): ?>
    <h1 class="notfound-header-search"><?php _e( 'Not Found Search', 'twentyten' ); ?></h1>
    <p class="notfound-content-search"><?php _e( 'Apologies, but no results were found.', 'twentyten' ); ?></p>
    <?php get_search_form(); ?>
<?php endif; ?>

I’m probably not including the is_search() correctly. But I’m working on this site: http://www.cirkut.net/wp/libertyguide and I want to show the search form when nothing is submitted (i.e. just clicking search from the homepage).

Read More

Any ideas as to what to do? Thanks for any help!

Related posts

Leave a Reply

3 comments

  1. I feel bad for answering my own question on here, but here’s what I did.

    I created a custom search template, a custom searchform.php and changed my header.php to reflect my custom search page.

    What I did is rename the search box names to search instead of s to get around WordPress automatically running search.php and coming up with a 404 error (still not sure why it happened, probably my fault in search.php) and then used a new WP_Query while setting my arguments. While my solution does not provide anything more than a search term, it could be easily implemented to pull other key-value pairs into the arguments array.

    searchform.php

    <div class="search">
        <form method="get" class="search-form" id="search-form" action="<?php bloginfo( 'url' ); ?>/search/">
        <div>
            <input class="search-text" type="text" name="search" id="search-text" value="Search this site" />
            <input class="search-submit" type="submit" name="submit" id="search-submit" value="Search" />
        </div>
        </form>
    </div>
    

    search-template.php snippet

    $s = wp_specialchars(stripslashes($_GET["search"]), 1);
    $search_query = array(
        's' => $s
    );
    
    $search = new WP_Query($search_query);
    

    So essentially s is now search to get around WordPress automatically using search.php.

    If anyone has any questions, feel free to post a comment.

  2. When doing a search with an empty string, WordPress will not do a search and therefore your search page will not be used. Within the $wp_query, a parameter, simply named s, represents the search term. If it has a value of '', WordPress does not direct this to the search page.

    As for how you can direct this to the empty search page, I am not sure. I know you could use pre_get_posts to set the s parameter, but that is likely a very hacky/sloppy solution, so hopefully others will be able to help you out with that part.

  3. i found a decent/easy jquery workaround on http://wpengineer.com/2162/fix-empty-searches/, preventing submit if the field is empty:

    /**
     * Stop empty searches
     *
     * @author Thomas Scholz http://toscho.de
     * @param  $ jQuery object
     * @return bool|object
     */
    (function( $ ) {
       $.fn.preventEmptySubmit = function( options ) {
           var settings = {
               inputselector: "#s",
               msg          : "Don’t waste your time with an empty search!"
           };
           if ( options ) {
               $.extend( settings, options );
           };
           this.submit( function() {
               var s = $( this ).find( settings.inputselector );
               if ( ! s.val() ) {
                   alert( settings.msg );
                   s.focus();
                   return false;
               }
               return true;
           });
           return this;
       };
    })( jQuery );
    

    And onload:

    jQuery( "#searchform" ).preventEmptySubmit();
    

    (This solution also shows an alert, but I prefer to disable it – and just keep focus on the search field…)