usually i’m typing a barcode or an sku number. so i’m fetching 1 result. when i’m not using this script the search results are taking about 2-3 seconds.
this is my script:
function cf_search_join( $join ) {
global $wpdb;
if ( is_search() ) {
$join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
}
return $join;
}
add_filter('posts_join', 'cf_search_join');
function cf_search_where( $where ) {
global $pagenow, $wpdb;
if ( is_search() ) {
$where = preg_replace(
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
"(".$wpdb->posts.".post_title LIKE $1 OR ".$wpdb->posts.".post_excerpt LIKE $1) OR (".$wpdb->postmeta. ".meta_value LIKE $1 AND ".$wpdb->postmeta. ".meta_key IN ('_barcode','_sku') )", $where );
}
return $where;
}
add_filter('posts_where', 'cf_search_where');
function cf_search_distinct( $where ) {
global $wpdb;
if ( is_search() ) {
return "DISTINCT";
}
return $where;
}
add_filter( 'posts_distinct', 'cf_search_distinct' );