Reply 319636 to: Ajax Search Product – adjust to search product, but not descriptions.

Avatar: Olga Barlow
Olga Barlow
Support staff
March 15, 2022 at 16:50

Hello,

There is no option to exclude description from the search. You can try to implement that by additional customization only. For example, you can try to add the below code to child theme functions.php

function search_by_title_only($search, $wp_query)
{
    global $wpdb;
    if (empty($search)) {
        return $search; // skip processing - no search term in query
    }
    $q = $wp_query->query_vars;
    $n = !empty($q['exact']) ? '' : '%';
    $search =
    $searchand = '';
    foreach ((array) $q['search_terms'] as $term) {
        $term = esc_sql($wpdb->esc_like($term));
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
    }
    if (!empty($search)) {
        $search = " AND ({$search}) ";
        if (!is_user_logged_in()) {
            $search .= " AND ($wpdb->posts.post_password = '') ";
        }
    }
    return $search;
}
add_filter('posts_search', 'search_by_title_only', 20, 2);

You can improve that in case you want to change anything else.

Regards

Go To The Whole Conversation In Topic
We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.