Search results are wrong
-
Hello, search results in the backend aren’t relevant.
For example when I am typing “régulateur”, I am getting another products before the product that has this title. I tested in a backup that I have and in this backup it works perfectly.
In addition, there is a search ajax fonctionnality included in the theme that is implemented and the search results are correct too (If we suppose that there is an issue with the database).
Could you please fix this issue ?
The page I need help with: [log in to see the link]
-
Hey there, @mitys! Thanks for contacting us. I’m happy to help you.
From what I understand, when you go to the Products section on your dashboard and use the search there, it does not show relevant products, correct?
I just checked on my website and I was not able to replicate the issue, so it does not seem this is a bug. Let’s investigate to figure out what is causing it on your website ??
I’d like to understand your site properly. Please share with us the necessary information below for us to investigate the issue further:
System Status Report which you can find via WooCommerce > Status > Get system report > Copy for support.
Fatal error logs (if any) under WooCommerce > Status > Logs. Then you can use the selector to search for Fatal Errors.
You could copy and paste your reply here or paste it via https://gist.github.com/ and send the link here.Looking forward to your reply.
Have a wonderful day!
Hi, here are the requested informations, thank you.
System Status Report:
https://gist.github.com/chahrazedturki/3272028aa15f2b2151b698342a4b8433
There is a fatal error but not related to the search action.
https://gist.github.com/chahrazedturki/c5b0e8844753ee20be0ca8818eb4a1e3
I think that search results are wrong because there is some keywords included in the excerpt of another products that influes the relevancy of the results. How to neglict the excerpts with the basic search field (without using another plugin) ?
Thank you.
Hi @mitys
WooCommerce’s built-in search functionality does include product excerpts and descriptions in the search criteria. This might be why you’re seeing products with the keyword in the excerpt appearing before the product with the keyword in the title.
If you want to limit the search to just product titles, you would need to modify the search query using custom code, as there’s no built-in option in WooCommerce to exclude excerpts from search results. I suggest checking this pull request’s comment which might help you further: https://github.com/woocommerce/woocommerce/pull/22165
Alternatively, you can ask for insight related to it on either the WooCommerce Advanced Facebook group or the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:
- A local web developer
- Codeable.io
- WooExperts
I hope this helps clarify the situation. Let me know if you have any other questions.
I found the solution finally, I customised the function with a filter, I'll post it here in case of someone else need it:
add_filter('woocommerce_product_pre_search_products', 'custom_woocommerce_product_pre_search_products', 10, 6);
function custom_woocommerce_product_pre_search_products( $custom_query=false, $term, $type, $include_variations, $all_statuses, $limit) {
? ? global $wpdb;
? ?// $custom_results = apply_filters( 'woocommerce_product_pre_search_products', false, $term, $type, $include_variations, $all_statuses, $limit );
? ? /*if ( is_array( $custom_results ) ) {
? ? ? ? return $custom_results;
? ? }
*/
? ? $post_types ? = $include_variations ? array( 'product', 'product_variation' ) : array( 'product' );
? ? $join_query ? = '';
? ? $type_where ? = '';
? ? $status_where = '';
? ? $limit_query ?= '';
? ? // When searching variations we should include the parent's meta table for use in searches.
? ? if ( $include_variations ) {
? ? ? ? $join_query = " LEFT JOIN {$wpdb->wc_product_meta_lookup} parent_wc_product_meta_lookup
? ? ? ? ?ON posts.post_type = 'product_variation' AND parent_wc_product_meta_lookup.product_id = posts.post_parent ";
? ? }
? ? /**
? ? ?* Hook woocommerce_search_products_post_statuses.
? ? ?*
? ? ?* @since 3.7.0
? ? ?* @param array $post_statuses List of post statuses.
? ? ?*/
? ? $post_statuses = apply_filters(
? ? ? ? 'woocommerce_search_products_post_statuses',
? ? ? ? current_user_can( 'edit_private_products' ) ? array( 'private', 'publish' ) : array( 'publish' )
? ? );
? ? // See if search term contains OR keywords.
? ? if ( stristr( $term, ' or ' ) ) {
? ? ? ? $term_groups = preg_split( '/\s+or\s+/i', $term );
? ? } else {
? ? ? ? $term_groups = array( $term );
? ? }
? ? $search_where ? = '';
? ? $search_queries = array();
? ? foreach ( $term_groups as $term_group ) {
? ? ? ? // Parse search terms.
? ? ? ? if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $term_group, $matches ) ) {
? ? ? ? ? ? if ($custom_WC_Product_Data_Store_CPT) {
? ? ? ? ? ? ? ? $search_terms = $custom_WC_Product_Data_Store_CPT->get_public_valid_search_terms($matches[0]);
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? $search_terms = array($term_group);
? ? ? ? ? ? }
? ? ? ? ? ? $count ? ? ? ?= count( $search_terms );
? ? ? ? ? ? // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence.
? ? ? ? ? ? if ( 9 < $count || 0 === $count ) {
? ? ? ? ? ? ? ? $search_terms = array( $term_group );
? ? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? ? $search_terms = array( $term_group );
? ? ? ? }
? ? ? ? $term_group_query = '';
? ? ? ? $searchand ? ? ? ?= '';
? ? ? ? foreach ( $search_terms as $search_term ) {
? ? ? ? ? ? $like = '%' . $wpdb->esc_like( $search_term ) . '%';
? ? ? ? ? ? // Variations should also search the parent's meta table for fallback fields.
? ? ? ? ? ? if ( $include_variations ) {
? ? ? ? ? ? ? ? $variation_query = $wpdb->prepare( " OR ( wc_product_meta_lookup.sku = '' AND parent_wc_product_meta_lookup.sku LIKE %s ) ", $like );
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? $variation_query = '';
? ? ? ? ? ? }
? ? ? ? ? $term_group_query .= $wpdb->prepare( " {$searchand} ( ( posts.post_title LIKE %s) OR ( wc_product_meta_lookup.sku LIKE %s ) $variation_query)", $like, $like ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
? ? ? ? ? $searchand ? ? ? ? = ' AND ';
? ? ? ? }
? ? ? ? if ( $term_group_query ) {
? ? ? ? ? ? $search_queries[] = $term_group_query;
? ? ? ? }
? ? }
? ? if ( ! empty( $search_queries ) ) {
? ? ? ? $search_where = ' AND (' . implode( ') OR (', $search_queries ) . ') ';
? ? }
? ? if ( ! empty( $include ) && is_array( $include ) ) {
? ? ? ? $search_where .= ' AND posts.ID IN(' . implode( ',', array_map( 'absint', $include ) ) . ') ';
? ? }
? ? if ( ! empty( $exclude ) && is_array( $exclude ) ) {
? ? ? ? $search_where .= ' AND posts.ID NOT IN(' . implode( ',', array_map( 'absint', $exclude ) ) . ') ';
? ? }
? ? if ( 'virtual' === $type ) {
? ? ? ? $type_where = ' AND ( wc_product_meta_lookup.virtual = 1 ) ';
? ? } elseif ( 'downloadable' === $type ) {
? ? ? ? $type_where = ' AND ( wc_product_meta_lookup.downloadable = 1 ) ';
? ? }
? ? if ( ! $all_statuses ) {
? ? ? ? $status_where = " AND posts.post_status IN ('" . implode( "','", $post_statuses ) . "') ";
? ? }
? ? if ( $limit ) {
? ? ? ? $limit_query = $wpdb->prepare( ' LIMIT %d ', $limit );
? ? }
? ? // phpcs:ignore WordPress.VIP.DirectDatabaseQuery.DirectQuery
? ? $search_results = $wpdb->get_results(
? ? ? ? // phpcs:disable
? ? ? ? "SELECT DISTINCT posts.ID as product_id, posts.post_parent as parent_id FROM {$wpdb->posts} posts
? ? ? ? ?LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON posts.ID = wc_product_meta_lookup.product_id
? ? ? ? ?$join_query
? ? ? ? WHERE posts.post_type IN ('" . implode( "','", $post_types ) . "')
? ? ? ? $search_where
? ? ? ? $status_where
? ? ? ? $type_where
? ? ? ? ORDER BY posts.post_parent ASC, posts.post_title ASC
? ? ? ? $limit_query
? ? ? ? "
? ? ? ? // phpcs:enable
? ? );
? ? $product_ids = wp_parse_id_list( array_merge( wp_list_pluck( $search_results, 'product_id' ), wp_list_pluck( $search_results, 'parent_id' ) ) );
? ? if ( is_numeric( $term ) ) {
? ? ? ? $post_id ? = absint( $term );
? ? ? ? $post_type = get_post_type( $post_id );
? ? ? ? if ( 'product_variation' === $post_type && $include_variations ) {
? ? ? ? ? ? $product_ids[] = $post_id;
? ? ? ? } elseif ( 'product' === $post_type ) {
? ? ? ? ? ? $product_ids[] = $post_id;
? ? ? ? }
? ? ? ? $product_ids[] = wp_get_post_parent_id( $post_id );
? ? }
? ? return wp_parse_id_list( $product_ids );
? ? }Hello @mitys,
Glad to know that the issue has been resolved now and thank you for sharing the solution and contributing to the WooCommerce Community.
I will mark this thread as resolved now. Should you have further inquiries, kindly?create a new topic here.
Meanwhile, if it isn’t too much to ask for – would you mind leaving us a review here? It only takes a couple of minutes but helps us tremendously. It would mean so much to us and would go a long way.
Thanks!
- You must be logged in to reply to this topic.