0

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.

4
  • Doesn't this answer your question? wordpress.stackexchange.com/questions/309800/…
    – bhanu
    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
    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
    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
    May 30, 2021 at 22:05

1 Answer 1

0

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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.