Hi all customer,
In this topic tutorial we’ll help you customize to search Product by title only in WooCommerce WordPress.
Open functions.php in your-site/wp-content/themes/{theme-slug}/functions.php ( example your-site/wp-content/themes/zumma-child/functions.php ) and copy custom PHP code below into end of that file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | if ( ! function_exists( 'lastudio_snippet__search_by_title_only' ) ) { function lastudio_snippet__search_by_title_only( $search, $wp_query ) { if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) && $wp_query->is_search && $wp_query->get('post_type') === 'product' ) { global $wpdb; $q = $wp_query->query_vars; $n = ! empty( $q['exact'] ) ? '' : '%'; $search = array(); foreach ( ( array ) $q['search_terms'] as $term ) { $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n ); } if ( ! is_user_logged_in() ) { $search[] = "$wpdb->posts.post_password = ''"; } $search = ' AND ' . implode( ' AND ', $search ); } return $search; } add_filter( 'posts_search', 'lastudio_snippet__search_by_title_only', 10, 2 ); } |
Hope this helps and thanks for reading!
Best Regards,
LA Team.