September 2, 2019 at 4:00 pm
#1133044
As Promised, the solution below changes the default search form to make it:
-Show products in a grid
-Add SKUs to the standard search
-Fixes the Ajax search so it all matches
Replace the search form
Edit the searchform.php (use your child theme to make a copy) Replace the default form with the following:
<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<div>
<input type="text" id="s" name="s" value="<?php if(!empty($_GET['s'])) echo get_search_query(); ?>" placeholder='<?php echo $search_params['placeholder']; ?>'>
<input type="submit" value="<?php echo $icon; ?>" id="searchsubmit" class="button <?php echo $class; ?>" />
<input type="hidden" name="post_type" value="product">
</div>
</form>
Add SKUs to the default search
The guy who put this together is brilliant thank you! Add the following to your child theme’s functions.php file:
/** __________________________________ ENHANCE THE STANDARD SEARCH
* Join posts and postmeta tables
* This one hels to include the SKU in tzhe standard search
* http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
*/
function cf_search_join( $join ) {
global $wpdb;
if ( is_search() || !empty($_REQUEST['s'] )) {
$join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->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() || !empty($_REQUEST['s'] ) ) {
$where = preg_replace(
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->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() || !empty($_REQUEST['s'] ) ) {
return "DISTINCT";
}
return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );
/* END Search includes SKU */
Hope this helps other people. It makes the Search in Enfold do what it should when Woocommerce is installed.
