Wordpress Search showing a blank page for empty results

admin

Administrator
Staff member
I've had to put together a custom search that sends the user to different search result pages based on which form they've filled out and the search also has been set to search custom post types.

When the search finds a result everything displays correctly but when it does an empty search it doesn't seem to be running the content-none.php template. Instead of "Sorry, but nothing matched your search terms. Please try again with some different keywords." It's just an empty page with the header and footer.

For an example you can go to: <a href="http://biozymeconnect.ev-labs.com/reseller/" rel="nofollow">http://biozymeconnect.ev-labs.com/reseller/</a> and in the second input use "tx" for a search with results and then something random for a search without results.

Details:

<ul>
<li>Base Theme: underscores </li>
<li>Framework: Foundation by Zurb</li>
</ul>

Custom Post Type search changes were made using this code <a href="http://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/" rel="nofollow">http://adambalee.com/search-wordpress-by-custom-fields-without-a-plugin/</a>

Code:
/**
 * Extend WordPress search to include custom fields
 *
 * http://adambalee.com
 */

/**
 * Join posts and postmeta tables
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
 */
function cf_search_join( $join ) {
    global $wpdb;

    if ( is_search() ) {    
        $join .=' LEFT JOIN '.$wpdb-&gt;postmeta. ' ON '. $wpdb-&gt;posts . '.ID = ' . $wpdb-&gt;postmeta . '.post_id ';
    }

    return $join;
}
add_filter('posts_join', 'cf_search_join' );

/**
 * Modify the search query with posts_where
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where
 */
function cf_search_where( $where ) {
    global $pagenow, $wpdb;

    if ( is_search() ) {
        $where = preg_replace(
            "/\(\s*".$wpdb-&gt;posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "(".$wpdb-&gt;posts.".post_title LIKE $1) OR (".$wpdb-&gt;postmeta.".meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( 'posts_where', 'cf_search_where' );

/**
 * Prevent duplicates
 *
 * http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_distinct
 */
function cf_search_distinct( $where ) {
    global $wpdb;

    if ( is_search() ) {
        return "DISTINCT";
    }

    return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );

To customize the search results I used this tutorial <a href="http://wpgarage.com/code-snippets/how-to-customize-multiple-search-result-pages-in-wordpress/" rel="nofollow">http://wpgarage.com/code-snippets/how-to-customize-multiple-search-result-pages-in-wordpress/</a>

edited the search.php to be

Code:
&lt;?php
/* Template Name: Search Results */
$search_refer = $_GET["site_section"];
if ($search_refer == 'reseller') { load_template(TEMPLATEPATH . '/reseller-search.php'); }
elseif ($search_refer == 'site-search') { load_template(TEMPLATEPATH . '/dealer-search.php'); }; ?&gt;

I edited the reseller-search.php and dealer-search.php to be.

Code:
&lt;?php
/**
 * The template for displaying search results pages.
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#search-result
 *
 * @package Biozyme_Connect
 */

get_header(); ?&gt;

    &lt;section id="primary" class="content-area"&gt;
        &lt;main id="main" class="site-main" role="main"&gt;

        &lt;?php
        if ( have_posts() ) : ?&gt;

            &lt;header class="page-header"&gt;
                &lt;h1 class="page-title"&gt;&lt;?php printf( esc_html__( 'Search Results for: %s', 'bzconnect' ), '&lt;span&gt;' . get_search_query() . '&lt;/span&gt;' ); ?&gt;&lt;/h1&gt;
            &lt;/header&gt;&lt;!-- .page-header --&gt;
            &lt;span style="background-color:#f87d20; display:block; padding:4px; color:#ffffff; font-weight:bold; text-transform: uppercase;"&gt;*Please note that you will be required to login after clicking the link to receive your discounted rates.&lt;/span&gt;
            &lt;table width="100%" class="search-results"&gt;
                &lt;thead&gt;
                    &lt;tr&gt;
                        &lt;th&gt;Name&lt;/th&gt;
                        &lt;th&gt;Company&lt;/th&gt;
                        &lt;th&gt;State&lt;/th&gt;
                        &lt;th&gt;Order&lt;/th&gt;
                    &lt;/tr&gt;
                &lt;/thead&gt;
                &lt;tbody&gt;
                    &lt;tr&gt;
                        &lt;?php
                        /* Start the Loop */
                        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( 'template-parts/content', 'search-reseller' );

                        endwhile;
                        echo '&lt;/tr&gt;
                &lt;/tbody&gt;
            &lt;/table&gt;';

                        the_posts_navigation();

                    else :

                        get_template_part( 'template-parts/content', 'none' );

                    endif; ?&gt;

        &lt;/main&gt;&lt;!-- #main --&gt;
    &lt;/section&gt;&lt;!-- #primary --&gt;

&lt;?php
get_footer();
?&gt;

content-none.php code

Code:
&lt;?php
/**
 * Template part for displaying a message that posts cannot be found.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package Biozyme_Connect
 */

?&gt;

&lt;section class="no-results not-found"&gt;
    &lt;header class="page-header"&gt;
        &lt;h1 class="page-title"&gt;&lt;?php esc_html_e( 'Nothing Found', 'bzconnect' ); ?&gt;&lt;/h1&gt;
    &lt;/header&gt;&lt;!-- .page-header --&gt;

    &lt;div class="page-content"&gt;
        &lt;?php
        if ( is_home() &amp;&amp; current_user_can( 'publish_posts' ) ) : ?&gt;

            &lt;p&gt;&lt;?php printf( wp_kses( __( 'Ready to publish your first post? &lt;a href="%1$s"&gt;Get started here&lt;/a&gt;.', 'bzconnect' ), array( 'a' =&gt; array( 'href' =&gt; array() ) ) ), esc_url( admin_url( 'post-new.php' ) ) ); ?&gt;&lt;/p&gt;

        &lt;?php elseif ( is_search() ) : ?&gt;

            &lt;p&gt;&lt;?php esc_html_e( 'Sorry, but nothing matched your search terms. Please try again with some different keywords.', 'bzconnect' ); ?&gt;&lt;/p&gt;
            &lt;?php
                get_search_form();

        else : ?&gt;

            &lt;p&gt;&lt;?php esc_html_e( 'It seems we can&amp;rsquo;t find what you&amp;rsquo;re looking for. Perhaps searching can help.', 'bzconnect' ); ?&gt;&lt;/p&gt;
            &lt;?php
                get_search_form();

        endif; ?&gt;
    &lt;/div&gt;&lt;!-- .page-content --&gt;
&lt;/section&gt;&lt;!-- .no-results --&gt;