How to Search Products by Custom Field in WooCommerce

How to Search Products by Custom Field in WooCommerce
On this page
  1. Why the Default Search Misses Custom Fields
  2. Step 1: Add the Custom Field Search Filter
  3. Step 2: Test With a Known Custom Field Value
  4. Troubleshooting
  5. Frequently Asked Questions
  6. Conclusion

You can search products by custom field in WooCommerce by filtering posts_search to also match a specific meta key’s value against the search term — extending the default search, which only checks the title, description, and optionally SKU.

Why the Default Search Misses Custom Fields

  • WooCommerce’s native frontend search only checks product title, short/long description, and (optionally) SKU
  • Custom fields like a brand, material, or model number aren’t included unless they happen to also appear in the description text
  • Shoppers searching by a value that’s stored in a custom field get a “no results” page even though a matching product exists
  • This is a common gap for catalogs that rely heavily on custom fields for attributes not modeled as taxonomy attributes

Step 1: Add the Custom Field Search Filter

This example searches a custom field called _brand — replace it with your actual meta key.

PHP
/** Extend product search to also match the _brand custom field */
add_filter( 'posts_search', 'extend_search_to_brand_custom_field' );

function extend_search_to_brand_custom_field( $where ) {
    global $wpdb, $wp;

    if ( is_admin() || ! is_search() || empty( $wp->query_vars['s'] ) || 'product' !== $wp->query_vars['post_type'] ) {
        return $where;
    }

    $search_term = $wp->query_vars['s'];

    $matches = $wpdb->get_col( $wpdb->prepare(
        "SELECT ID FROM {$wpdb->posts}
         LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id
         WHERE meta_key = '_brand' AND meta_value LIKE %s",
        '%' . $wpdb->esc_like( wc_clean( $search_term ) ) . '%'
    ) );

    $matches = array_filter( array_unique( array_map( 'absint', $matches ) ) );

    if ( $matches ) {
        $where = str_replace(
            'AND (((',
            "AND ( ({$wpdb->posts}.ID IN (" . implode( ',', $matches ) . ")) OR ((", // Same number of open brackets as the original "AND ((("
            $where
        );
    }

    return $where;
}
Search products by custom field in WooCommerce: the page before adding the code
Before
Search products by custom field in WooCommerce: the page after adding the code
After

Step 2: Test With a Known Custom Field Value

Search for a value you know exists in the target custom field on at least one product, confirming it now appears in results where it previously returned nothing.

Troubleshooting

Search Returns No Results Even With a Known Value

Confirm the exact meta key spelling — a leading underscore matters (_brand is different from brand), and it must match precisely how the field is actually stored in the database.

The str_replace Pattern Doesn’t Match Correctly

WordPress’s core search SQL structure can vary slightly between versions — if the replacement doesn’t apply cleanly, inspect the actual generated $where clause via debugging to confirm the exact string pattern your WordPress version produces.

Frequently Asked Questions

Can I search multiple custom fields at once?

Yes — expand the SQL query’s meta_key condition to use IN ('_brand', '_model') or run multiple queries and merge the resulting ID arrays.

Does this affect the WordPress admin search too?

No — the is_admin() check specifically excludes backend search, so this only extends the frontend shop search experience.

Will this work with taxonomy-based attributes instead of custom fields?

Not directly — taxonomy attributes are stored differently (as terms, not post meta), so searching those requires a taxonomy query rather than this meta-based approach.

Conclusion

Extending WooCommerce’s frontend search to check a custom field closes a real gap for stores relying on meta fields for product attributes that aren’t otherwise searchable. Test with a known value first, and double-check the exact meta key spelling if results don’t appear as expected. Getting this right is really what search products by custom field in woocommerce comes down to.

See also How to Show Product Search Suggestions in WooCommerce. Then read How to Search Products by SKU on the WooCommerce Frontend.

Prefer no code? All-in-One Utilities Woo Add-on adds over 50 WooCommerce features, so you can switch them on without code.

Leave a Reply

Schedule a call