admin管理员组文章数量:1122846
i have a woocommerce e-shop website. My search is searching only in product title, short description and SKU fields. I want to search also in some custom fields (like "_barcode" and "_mpn"). I want to search on products pages, not in admin panel.
I have this script but when i use it in fuctions.php i can't search by product title or short description.
function search_filter( $query ) {
$key_fields = array ( '_barcode', '_mpn', '_sku' );
$value_field = $query->query_vars['s'];
$query->query_vars['s'] = '';
if ( !is_admin() && $value_field != '' ) {
$filter_query = array( 'relation' => 'OR' );
foreach ( $key_fields as $one_field ) {
array_push ( $filter_query , array (
'key' => $one_field,
'value' => $value_field,
'compare' => 'LIKE'
) );
}
$query->set( 'meta_query' , $filter_query );
}
}
add_filter( 'pre_get_posts' , 'search_filter');
Why i can't search by product title or short description when i use this script? Any idea?
Thank you.
i have a woocommerce e-shop website. My search is searching only in product title, short description and SKU fields. I want to search also in some custom fields (like "_barcode" and "_mpn"). I want to search on products pages, not in admin panel.
I have this script but when i use it in fuctions.php i can't search by product title or short description.
function search_filter( $query ) {
$key_fields = array ( '_barcode', '_mpn', '_sku' );
$value_field = $query->query_vars['s'];
$query->query_vars['s'] = '';
if ( !is_admin() && $value_field != '' ) {
$filter_query = array( 'relation' => 'OR' );
foreach ( $key_fields as $one_field ) {
array_push ( $filter_query , array (
'key' => $one_field,
'value' => $value_field,
'compare' => 'LIKE'
) );
}
$query->set( 'meta_query' , $filter_query );
}
}
add_filter( 'pre_get_posts' , 'search_filter');
Why i can't search by product title or short description when i use this script? Any idea?
Thank you.
Share Improve this question asked May 28, 2021 at 23:19 KyriakosKyriakos 11 bronze badge 4- Doesn't this answer your question? wordpress.stackexchange.com/questions/309800/… – bhanu Commented May 30, 2021 at 8:13
- i tried and it's working but it's very slow. it's taking about 40 seconds to display the results. – Kyriakos Commented May 30, 2021 at 19:21
- How many results are you fetching? 40 seconds too long a time for a very crappy server too. – bhanu Commented May 30, 2021 at 19:56
- 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. – Kyriakos Commented May 30, 2021 at 22:05
1 Answer
Reset to default 0usually 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' );
本文标签: meta queryWoocommerce products search with custom fields
版权声明:本文标题:meta query - Woocommerce products search with custom fields 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289686a1928358.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论