Product search not checking “product_tag”?

  • Open
  • avtars
    Participant
    November 15, 2019 at 4:47 am #4174
    avtars

    It appears as tho the search functionality for WooCommerce products does not check “product_tag” taxonomy terms, nor SKU field? How i can include these fields in the search?

  1. Manik Malhotra
    Keymaster
    Posts: 48
    November 15, 2019 at 11:14 am #4182
    Manik Malhotra

    It is too simple just paste the following code in function.php and then you can see the changes in search results.

    
    
    function search_by_id_only( $search, &$query_vars ) {
    global $wpdb, $pagenow;
    
    if ( 'edit.php' == $pagenow || empty($search) ) {
    return $search;
    }
    
    $args = array(
    'posts_per_page' => -1,
    'post_type' => 'product',
    'meta_query' => array(
    array(
    'key' => '_sku',
    'value' => $query_vars->query['s'],
    'compare' => 'LIKE'
    )
    )
    );
    $posts = get_posts( $args );
    if ( empty( $posts ) ) return $search;
    $get_post_ids = array();
    foreach($posts as $post){
    $get_post_ids[] = $post->ID;
    }
    if ( sizeof( $get_post_ids ) > 0 ) {
    $search = str_replace( 'AND (((', "AND ((({$wpdb->posts}.ID IN (" . implode( ',', $get_post_ids ) . ")) OR (", $search);
    }
    return $search;
    }
    add_filter( 'posts_search', 'search_by_id_only', 999, 2 );