0

hi i need your help regarding for woo-commerce product title based search we have list of product my searching keyword available in product title need to show list product. if product title don't have my search keyword but available in product description section this don't need to display this product.

0

add below code on function.php file in your theme directory .

<?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( like_escape( $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', 500, 2 );
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.