WooCommerce: Search By Custom Field (Frontend)

The default WooCommerce frontend product search returns results based on whether the search term is present in the product title, short and long description. Also, you can optionally search products by SKU.

But what if you also want to search for a custom field value e.g. you have a custom field called “_brand” and you want to get the products where “_brand” is equal to “apple“?

Now, I’m not sure I’ve explained this in plain English, so let’s take a look at a practical example. Enjoy!

With the snippet below, and if I had “puma” saved in the “_brand” custom field for some products, this search result would actually return those products!

PHP Snippet: Search Products By Custom Field Value @ WooCommerce Search

The only thing you need to change in the snippet below is ‘_brand‘. Change it to the key of your custom field, and you should get results based on that. Let me know in the comments if it works!

 * @snippet       Search By Custom Field @ WooCommerce Search
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/

add_filter( 'posts_search', 'bbloomer_product_search_by_custom_field' );

function bbloomer_product_search_by_custom_field( $where ) {
    global $wpdb, $wp;
    if ( is_admin() || ! is_search() || ! isset( $wp->query_vars['s'] ) || 'product' != $wp->query_vars['post_type'] ) {
        return $where;
    $product_ids = array();
    $terms = explode( ',', $wp->query_vars['s'] );
    foreach ( $terms as $term ) {		
		$products = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_parent FROM {$wpdb->posts} LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id WHERE meta_key IN ( '_brand' ) AND meta_value LIKE %s;", '%' . $wpdb->esc_like( wc_clean( $term ) ) . '%' ) );       
		$products = array_merge( wp_list_pluck( $products, 'ID' ), wp_list_pluck( $products, 'post_parent' ) );
        if ( sizeof( $products ) > 0 ) {
            $product_ids = array_merge( $product_ids, $products );
    $product_ids = array_filter( array_unique( array_map( 'absint', $product_ids ) ) );
	if ( sizeof( $product_ids ) > 0 ) {
        $where = str_replace( 'AND (((', "AND ( ({$wpdb->posts}.ID IN (" . implode( ',', $product_ids ) . ")) OR ((", $where );
    return $where;	

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]
  • WooCommerce: Add Custom Product Fields (e.g. RRP) Without a Plugin
    The manufacturer’s suggested retail price (MSRP), or the recommended retail price (RRP), is the price at which the manufacturer recommends that the retailer sells the product at. You might have seen this in an ad, on a magazine, on a price tag: “RRP: $50. Our price: $39!”. WooCommerce entrepreneurs can take advantage of this “marketing […]
  • WooCommerce: Show Product Custom Field in the Category Pages
    A client asked me to show a given custom field in the loop (i.e. Shop page, Category pages, Tag pages = anywhere woocommerce products are returned). Interestingly enough, she didn’t want to show the product short description (see “show product short description on the homepage only” snippet) but a custom field, so here’s how you […]
  • WooCommerce: Add a Product Search Bar in the Header/Footer
    Hola amigos, today’s snippet actually comes from my own website. You might have noticed there is a little “magnifying glass” in the navigation menu which scrolls down to a search bar. Mine, specifically, searches exclusively for blog posts (I excluded pages, products, etc.), but you can customize this and make it search for products only […]
  • WooCommerce: Multi-Vendor / Marketplace Solutions
    Building the next Amazon is everyone’s dream. Allowing sellers to use your online platform to reach wider audiences without holding stock, investing in shipping and warehousing and – let’s be honest – with a few dollars budget… is actually possible in WooCommerce. The WooCommerce Multi-Vendor/Marketplace scenarios are many – not a surprise. And sometimes, a […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

4 thoughts on “WooCommerce: Search By Custom Field (Frontend)

  1. thanks this worked for me. So, is it possible to add more than one meta field to this structure?

    1. Hello Mehmet, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Hi there,

    When I use your code in a Code Snippet, it generates a warning in the logs:

    "PHP Warning:  Undefined array key "post_type" in ..."

    Any suggestions to improve this code?

    Kind regards,


    1. I don’t see any “post_type” array key in my snippet, so this may be because of something else (a plugin or theme)

Questions? Feedback? Customization? Leave your comment now!

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *