SOLVED: Woocommerce search by SKU

Can anyone tell how to use new built-in filter/search functionality to search products by SKU.

1 Like

Second that. Would be good to have in the Products Filter Search. My global search in WP already returns them fine (I’m using a code snippet to make that happen) but it’s not working on this Bricks Element.

I third that.

@Allucinante Would you be willing to share your code snippet? I have a site-wide search, but it only pulls from product title, not SKU, description, or Tag (which all would be useful.)

TIA

To search by SKU, you need to modify the default WooCommerce search. You can achieve this by installing the Relevanssi Light plugin.

1 Like

Here you go. I’m using a modified version, based on this on. Remember, this only works with the WP Search widget, not in the product search of the Bricks products filter element:

1 Like

guys _sku is just a custom field

if you want to make custom field filtering with _sku s here this is the setup;

and if you want to add the _sku field to the normal site global search here use this snippet.
enjoy


// Function to modify the search query to include SKU in WooCommerce product searches
function search_by_sku_in_woocommerce( $search, $wp_query ) {
    global $wpdb;

    // Check if it's a search query and WooCommerce is active
    if ( ! $wp_query->is_search() || ! $wp_query->is_main_query() || ! is_search() || ! class_exists( 'WooCommerce' ) ) {
        return $search;
    }

    // Get the search query
    $search_term = $wp_query->query_vars['s'];

    // Only modify the search if there is a search term
    if ( ! empty( $search_term ) ) {
        // Get the current search query parts
        $search = " AND ({$wpdb->posts}.post_title LIKE '%" . esc_sql( $search_term ) . "%'
                    OR {$wpdb->posts}.post_content LIKE '%" . esc_sql( $search_term ) . "%'
                    OR EXISTS (
                        SELECT 1 FROM {$wpdb->postmeta}
                        WHERE {$wpdb->postmeta}.meta_key = '_sku'
                        AND {$wpdb->postmeta}.meta_value LIKE '%" . esc_sql( $search_term ) . "%'
                        AND {$wpdb->postmeta}.post_id = {$wpdb->posts}.ID
                    ))";
    }

    return $search;
}
// Hook the function into the 'posts_search' filter
add_filter( 'posts_search', 'search_by_sku_in_woocommerce', 10, 2 );


2 Likes

Thanks for the snippet!

I’ve added this to my website via code snippet plugin and activated, but it’s still not searching by SKU


interesting I tested on my woocommerce site and it works fine
I cant say why didn’t work for you.

¯_(ツ)_/¯

don’t use any other search plugin top of it :slight_smile:

One possible solution is to manually rebuild the WordPress search index. However, the exact steps may vary depending on your specific setup and any plugins you are using for search functionality. If you are using a search plugin like FiboSearch, SearchWP, Relevanssi, etc., check the plugin settings or documentation for an option to rebuild the index.

Hey everyone,

I wanted to share my experience trying to enable SKU search functionality in Bricks Builder, specifically for filtering a query loop of WooCommerce products.

Unfortunately, I still can’t get it to work.


:mag: Goal:

Allow customers to search by SKU using the Bricks Filter – Search element tied to a product loop.


:white_check_mark: What does work:

I used a PHP function to sync each product’s SKU into a searchable custom meta field (_searchable_sku), and confirmed it works when I filter the query directly by that key.

Here’s the working meta sync snippet:

php

// Save SKU to a searchable custom field
function sync_sku_to_searchable_field($product_id) {
    $product = wc_get_product($product_id);
    if ($product && $product->get_sku()) {
        update_post_meta($product_id, '_searchable_sku', $product->get_sku());
    } else {
        delete_post_meta($product_id, '_searchable_sku');
    }
}
add_action('woocommerce_update_product', 'sync_sku_to_searchable_field');
add_action('save_post_product', 'sync_sku_to_searchable_field');

// One-time bulk sync for existing products
function set_initial_searchable_sku_meta() {
    $args = ['post_type' => 'product', 'posts_per_page' => -1];
    $products = get_posts($args);
    foreach ($products as $product) {
        sync_sku_to_searchable_field($product->ID);
    }
}
// Uncomment to run once
// add_action('init', 'set_initial_searchable_sku_meta');

With this, I was able to visually filter a Bricks query loop by setting:

  • Meta key: _searchable_sku
  • Compare: =
  • Value: SKU1234 (or any real SKU)

Bricks returns the correct product :white_check_mark:


:x: What doesn’t work (so far):

Using Bricks’ Filter – Search element with this setup to dynamically search by SKU does not return results.

I tried multiple approaches:

  1. Custom Query Loop with PHP Query Editor – captured $_GET search param, added a meta_query manually.
  2. Dynamic token {{ url_parameter.xyz }} in a meta query filter – does not inject the value.
  3. pre_get_posts filter in functions.php – confirmed param was passed correctly but no results returned.
  4. Even tried case-insensitive meta queries and debug logging — all confirmed the search value existed, but the query loop wouldn’t return results unless hardcoded.

Conclusion:

  • Bricks can successfully filter a loop by SKU using _searchable_sku, but not dynamically via Filter – Search.
  • Possibly due to how Bricks handles AJAX/dynamic filters or token parsing into the query.

:question: Open question:

If anyone has been able to successfully enable dynamic SKU search (e.g., customer types SKU into Bricks Filter – Search and gets product results), I’d love to hear how you approached it!

Happy to test further or provide more details if it helps.

Hi @updigitalstudio ,

You can try this

Thank you! I can’t believe it, I enabled “Query Bricks data in search results” and it works now, no additional code needed.

I can now search SKU using the Bricks Builder Filter Search for a query loop. :white_check_mark:

@updigitalstudio

Unfortunately, that is not right. That is a bug. Turning that feature on shouldn’t make the SKU search work. It should just search from the Bricks data.

It will be fixed in Bricks siib. Don’t use it as a “feature” to search SKU :slight_smile:

Instead, you should try the snippets I shared there.

Thanks @itchycode! I’ve disabled it and then implemented the following and it worked! Much appreciated.

:white_check_mark:

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