WooCommerce WP_Query to search title and SKU

I am using the following args to search WooCommerce products in the title or the SKU.

The search should do a partial match on the search term e.g.‘timb’ would match ‘Timber chair’ in the title or ‘M-TIMB’ in the SKU.

I am trying the below but it seems to search for products that have the search term in both the title and SKU

What is the correct $args to search for a partial match in the SKU or title?

// Query products matching the search string in their SKU or title
$args = [
    'post_type'      => 'product',          // Search in 'product' post type
    'post_status'    => 'publish',          // Only published products
    'posts_per_page' => 20,                 // Limit results to 20 per page
    's'              => $search,            // Search term in title and content
    'meta_query'     => [
        'relation' => 'OR',                 // Either SKU or title
        [
            'key'     => '_sku',
            'value'   => $search,
            'compare' => 'LIKE'               // Partial match on SKU
        ]
    ],
    'fields'         => 'ids'               // Only get post IDs to optimize performance
];

Note this searches the title correctly:

$args = [
    'post_type'      => 'product',
    'post_status'    => 'publish',
    's'              => $search, // Search term
    'posts_per_page' => 20,      // Adjust if you need to limit results
];

And this searches the SKU correctly:

$args = [
    'post_type'      => 'product',          // Search in 'product' post type
    'post_status'    => 'publish',          // Only published products
    'posts_per_page' => 20,                 // Limit results to 20 per page
    'meta_query'     => [                   // Custom fields query (e.g., SKU)
        [
            'key'     => '_sku',
            'value'   => $search,
            'compare' => 'LIKE'               // Partial match on SKU
        ]
    ],
    'fields'         => 'ids'               
];

If I combine the two as separate calls I get the answer I want but I want to do it with one call.


Based on the comment below I have modified args to:

$args = [
    'post_type'      => 'product',          
    'post_status'    => 'publish',          
    'posts_per_page' => 20,                 

    'title_filter' => $search,          // new
    'title_filter_relation' => 'OR',    // new

    'meta_query'     => [
        'relation' => 'OR',          
        [
            'key'     => '_sku',
            'value'   => $search,
            'compare' => 'LIKE'             
        ]
    ],
    'fields'         => 'ids'             
];

but it still returns and AND match

The issue with your current $args is that you’re using an OR relation in the meta_query and also setting the title_filter and title_filter_relation . This creates a complex condition that might not be producing the desired results.

To achieve an OR match on both the title and SKU, you should use a single meta_query with an OR relation and include both the post_title and _sku fields.

Here’s the corrected $args :

PHP

$args = [
    'post_type'      => 'product',
    'post_status'    => 'publish',
    'posts_per_page' => 20,
    'meta_query'     => [
        'relation' => 'OR',
        [
            'key'     => 'post_title',
            'value'   => $search,
            'compare' => 'LIKE'
        ],
        [
            'key'     => '_sku',
            'value'   => $search,
            'compare' => 'LIKE'
        ]
    ],
    'fields'         => 'ids'
];

This will search for products where the search term matches either the post_title or the _sku field, effectively performing an OR match.