Search only in product titles - by MAHDAR

This topic has 2 replies, 2 voices, and was last updated 3 months, 3 weeks ago ago by Luca Rossi

  • Avatar: mahdar
    MAHDAR
    Participant
    August 14, 2025 at 16:28

    One thing I’ve noticed is that when a user searches for a product — for example, “shoes” — if the word “shoes” appears in the description of another product, such as a suit, that suit will also appear in the results.

    Is it possible to make the search function only check product titles instead of descriptions?

    Please, contact administrator
    for this information.
    1 Answer
    Avatar: Justin
    Luca Rossi
    Support staff
    August 15, 2025 at 06:20

    Dear @MAHDAR,

    Thank you for reaching out to us.

    Please note that implementing the functionality you are requesting would require extensive customization, which unfortunately falls outside the scope of the standard support services we provide.

    However, as a courtesy, we are pleased to share a sample custom code snippet that you may review and modify according to your specific requirements:

    
    <?php
    /**
     * Hook WooCommerce search to search by title only
     * Add this to your theme's functions.php
     */
    
    // Remove WooCommerce default search behavior
    remove_action('pre_get_posts', 'woocommerce_product_search_pre_get_posts');
    
    // Add custom search behavior - title only
    function custom_woocommerce_search_title_only($query) {
        if (!is_admin() && $query->is_main_query()) {
            // Check if it's a product search
            if (is_search() && isset($_GET['post_type']) && $_GET['post_type'] === 'product') {
                // Remove meta and taxonomy queries that WooCommerce adds
                $query->set('meta_query', array());
                $query->set('tax_query', array());
                
                // Only search in post title
                add_filter('posts_search', 'search_by_title_only', 500, 2);
            }
        }
    }
    add_action('pre_get_posts', 'custom_woocommerce_search_title_only');
    
    // Modify the search SQL to only search in post_title
    function search_by_title_only($search, $wp_query) {
        global $wpdb;
        
        if (empty($search)) {
            return $search;
        }
        
        $q = $wp_query->query_vars;
        $n = !empty($q['exact']) ? '' : '%';
        $search_term = $n . $wpdb->esc_like($q['s']) . $n;
        
        $search = $wpdb->prepare(" AND ({$wpdb->posts}.post_title LIKE %s) ", $search_term);
        
        return $search;
    }
    

    We hope this example proves helpful. Should you require further assistance with custom development, we recommend consulting a professional developer.

    Best regards,
    The 8Theme Team

  • Viewing 2 results - 1 through 2 (of 2 total)

The issue related to '‘Search only in product titles’' has been successfully resolved, and the topic is now closed for further responses

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.