Can anyone tell how to use new built-in filter/search functionality to search products by SKU.
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.
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:
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 );
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
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.
Goal:
Allow customers to search by SKU using the Bricks Filter â Search element tied to a product loop.
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
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:
- Custom Query Loop with PHP Query Editor â captured
$_GET
search param, added a meta_query manually. - Dynamic token
{{ url_parameter.xyz }}
in a meta query filter â does not inject the value. pre_get_posts
filter infunctions.php
â confirmed param was passed correctly but no results returned.- 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.
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.
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
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.
// 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 );