This is very common and normal behaviour for WooCommerce. The search mechanism considers product descriptions or other fields beyond just the product title in other products, so if you have other items in the shop with the same name in the title “iPhone 12, iPhone 13, etc” or in the product short/long descriptions, this would occur.
To ensure search results are more relevant and focused on the keywords entered, you might need to adjust how the search query is processed on your site.
Here are several approaches to refine the search functionality:
Custom code: This can be achieved with custom code added to your child theme’s functions.php
file:
function custom_search_exact_match($search, $wp_query) {
global $wpdb;
if (!empty($search) && !is_admin() && $wp_query->is_search && $wp_query->is_main_query()) {
$keywords = $wp_query->query_vars['s'];
// Modify the search query to search for an exact match in post titles.
$search = " AND {$wpdb->posts}.post_title = '" . esc_sql($wpdb->esc_like($keywords)) . "'";
}
return $search;
}
add_filter('posts_search', 'custom_search_exact_match', 10, 2);
This code is quite simplistic and makes the search strictly match the exact title, which might be too restrictive for some use cases. It’s a starting point and might need adjustments to fit your exact needs.
Or.. Use a Custom Search Plugin:
Consider using a search plugin like SearchWP or Relevanssi. These plugins offer advanced control over the search algorithm, including the ability to set “exact match” rules. You can configure them to prioritize exact matches in product titles over partial matches or content found in descriptions and other metadata.
Good luck mate!