• Resolved anggidoran

    (@anggidoran)


    When I search for a product using the search bar, the results do not match the keyword I entered. For example, when I type for “Iphone 15”, the results show products such as “Iphone 15” and several other products that are not relevant to the keyword. https://prnt.sc/SvqMB_n1vxsr

    I expected the search results should only show products that are relevant to the keyword entered.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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!

    Thread Starter anggidoran

    (@anggidoran)

    Thank you for your help. I modified it according to your suggestion, and finally the problem is solved.

    Plugin Support ckadenge (woo-hc)

    (@ckadenge)

    Hi @anggidoran,

    Thank you for your help. I modified it according to your suggestion, and finally the problem is solved.

    I’m glad we were able to help!

    If you have a few minutes, we’d love if you could leave us a review: https://www.ads-software.com/support/plugin/woocommerce/reviews/

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Product Search Result Issue (the results do not match)’ is closed to new replies.