Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Search WooCommerce Products by Custom Field ?

Searching WooCommerce products in the admin area is usually limited to product names and SKUs. But what if you want to search products by a custom field or meta key? This can be especially useful for stores with large product catalogs or custom product identifiers.

In this guide, we’ll show you how to customize the WooCommerce admin product search to include custom fields, making it faster and easier to find the products you need.

Solution: Search WooCommerce Products by Custom Field

The following code snippet allows you to search WooCommerce products in the admin by a custom field value. It works for both simple products and variations and ensures results only appear on the product listing page.

Note: If your store uses a custom field name (for example, vendor_code or supplier_id), update the snippet accordingly by replacing the meta_key 'supplier_id' with your actual meta key name.

add_filter('woocommerce_product_pre_search_products', 'ts_search_products_by_supplier_id', 10, 6);

function ts_search_products_by_supplier_id($results, $term, $type, $include_variations, $all_statuses, $limit) {
    if (!is_admin() || !function_exists('get_current_screen')) {
        return $results;
    }

    $screen = get_current_screen();
    if (!$screen || $screen->id !== 'edit-product') {
        return $results;
    }

    $term = trim((string)$term);
    if ($term === '') {
        return $results;
    }

    global $wpdb;

    $post_statuses = current_user_can('edit_private_products') ? ['private', 'publish'] : ['publish'];
    $post_types    = $include_variations ? ['product', 'product_variation'] : ['product'];

    $like = '%' . $wpdb->esc_like($term) . '%';
    $limit_sql = $limit ? $wpdb->prepare('LIMIT %d', (int)$limit) : 'LIMIT 1000';

    $types_in   = implode("','", array_map('esc_sql', $post_types));
    $status_in  = implode("','", array_map('esc_sql', $post_statuses));

    $sql = "
        SELECT DISTINCT
            CASE p.post_type WHEN 'product_variation' THEN p.post_parent ELSE p.ID END AS product_id
        FROM {$wpdb->posts} p
        INNER JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID
        WHERE p.post_type IN ('{$types_in}')
          AND p.post_status IN ('{$status_in}')
          AND pm.meta_key   = %s
          AND pm.meta_value LIKE %s
        {$limit_sql}
    ";

    $ids = $wpdb->get_col($wpdb->prepare($sql, 'supplier_id', $like));

    if (empty($ids)) {
        return $results;
    }

    return array_values(array_unique(array_map('intval', $ids)));
}

Output

Consider a WooCommerce store that manages multiple suppliers and has a custom field supplier_id, you can search for products in the admin panel directly by their supplier ID values.

WooCommerce products custom field

Go to WooCommerce → Products → All Products, and type the supplier code MOTO-IND-09 into the search box and hit Search. Instantly, the products supplied by MOTO Enterprise appear in the list, showing me their names, SKUs, prices, and other product details.

search WooCommerce products by custom field

Now that you can search WooCommerce products by custom field values, managing your catalog becomes much easier, especially if you deal with a large catalog of products. Want to add these fields to products or variations? You can easily add custom fields to your product variations and display specific messages or information for each variation.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Leave a Reply

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

Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible.

Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

By using our site, you acknowledge that you have read and understood our Privacy Policy and Terms & Conditions.