WIP: Search filter returning irrelevant results once enabled "Query Bricks data in search results"

I use several query filters on this page of my Woocommerce site: https://coloursbykarat.com/shop/

One of the filters is a “Filters - Search” element. When entering a value such as “0387” (intended to show all products where the SKU contains “0387”), the grid shows the 3 products that have “0387” in their SKU (GOOD), but also any product that has an upsell or a cross-sell that has “0387” in the SKU (BAD). The url of the page after the filter is applied is https://coloursbykarat.com/shop/?posts_per_page=48&s=0387.

If I refresh the page, or copy and paste this URL into a new browser window, only the 3 products with “0387” SKUs are shown (GOOD).

Hi Eric,
Thanks so much for your report!

Can you briefly check if “Query Bricks data in search results” is enabled and if disabling it makes a difference?

Best regards,
timmse

“Query Bricks data in search results” WAS enabled, and disabling it DID make a difference (when disabled, NO results were returned when searching for “0387”).

Hi @ainom ,

The expected result should be 0

The default WP search will not search through the SKU meta field (_sku)

I have created the “Query Bricks data in search results” bug for the dev team. Currently once you enabled this, it will amend to search the value from all post meta key which is not right.

By the way, if you want to amend WP default query to search the SKU, you should use PHP filter like “posts_where” hook etc.

Regards,
Jenn

I use SearchWP plugin which includes the SKU in searches. You can tell that it work by doing a basic search with a URL like https://coloursbykarat.com/?s=0387.

Hi @ainom ,

Do you mean you cannot get the same result even if “Query Bricks data in search results” is disabled?

If so, it could be that the plugin is not compatible with Bricks Query Filters queries. (In Bricks custom rest endpoint)

You can send us admin credentials for checking.

Regards,
Jenn

Do you mean you cannot get the same result even if “Query Bricks data in search results” is disabled?

Correct.

I will email the login credentials.

Hi @ainom ,

As per our email conversation, the SearchWP issue is a different topic. It seems like their Search algorithm will not be executed if the request/search query is performed on a custom REST API endpoint.

You just need to find a way to search Woo SKU when performing Bricks Query Filters and default search. This is the working code snippet applied to your site. Sharing it here in case anyone needs it.

// Search SKU
add_filter( 'posts_where', function( $where, $query ) {

	if ( $query->get( 'brx_is_search' ) || is_search() ) {
		global $wpdb;
		$search_term = $query->get( 's' );
		if ( $search_term && $search_term !== '' ) {
			$where = preg_replace_callback(
				'/\(\s*' . preg_quote( $wpdb->posts . '.post_title', '/' ) . '\s+LIKE\s*(\'[^\']+\')\s*\)/',
				function( $matches ) use ( $wpdb ) {
					$like = $matches[1];
					return '(' . $wpdb->posts . ".post_title LIKE $like OR (woosku.meta_key = '_sku' AND woosku.meta_value LIKE $like))"; 
				},
				$where
			);
		}
	}

	return $where;
}, 10, 2 );
	
	
// Search SKU
	
add_filter( 'posts_join', function ( $join, $query ) {
	if ( $query->get( 'brx_is_search' ) || is_search() ) {
		global $wpdb;
		$search_term = $query->get( 's' );
		if ( $search_term && $search_term !== '' ) {
			$join .= ' LEFT JOIN ' . $wpdb->postmeta . ' woosku ON ' . $wpdb->posts . '.ID = woosku.post_id ';
		}
	}

	return $join;
}, 10, 2 );
	
	
// Search SKU
add_filter( 'posts_distinct', function ( $where, $query ) {
	if ( $query->get( 'brx_is_search' ) || is_search() ) {
		$search_term = $query->get( 's' );
		if ( $search_term && $search_term !== '' ) {
			return 'DISTINCT';
		}
	}
	
	return $where;
	
}, 10, 2 );

Regards,
Jenn

Disabling it likely removed a key data source