Learn how plugin search and order product results.
All plugin search results order by relevance score. This score is calculated based on where the searched word is placed relative to product content ( title, description, excerpt, ... ).
So, if a searched word is found inside the product title then this product will be placed higher inside search results than the product that has the same word inside its description.
The plugin also takes into account the number of mentions of each word within the product. More mentions means a higher product in search results.
Also, the search query that fully matches the words inside the product has higher relevance than partial matching.
For example, user search for cap
. We have two products inside our WooCommerce store: Cap and Sunglasses with capacity. So the first product title word fully matches with the search query and the second one has only partial match. First product will have a higher relevance score and will be displayed higher inside search results.
The weight of each product field is initially set within the plugin. But it is also possible to change it with some custom code snippets.
Example 1: we want to add more weight to words inside product content and make them more important than words inside product title. This can be done by using aws_relevance_scores
filter. Code snippet can looks like this:
add_filter( 'aws_relevance_scores', 'my_aws_relevance_scores' ); function my_aws_relevance_scores( $relevance_array ) { $relevance_array['title'] = 100; $relevance_array['content'] = 350; return $relevance_array; }
Example 2: we want to add more relevance to the specific product itself. For example, we have a product with ID 123
and we want to always show it at the top of search results.
add_filter( 'aws_search_query_array', 'my_aws_search_query_array' ); function my_aws_search_query_array( $query ) { $relevance = '( case when ID = 123 then 800 else 0 end ) + '; $query['relevance'] = preg_replace( '/(SUM([sS]*?([sS]*?case[sS]*?end[sS]*?)[sS]*?+/i', '$0' . $relevance, $query['relevance'] ); return $query; }
Example 3: lets add more relevance for products with specific categories. For example, we have product categories with slugs term1
and term2
. We want to add a more relevance score for products that have any of this category.
add_filter( 'aws_search_query_array', 'my_aws_search_query_array' ); function my_aws_search_query_array( $query ) { $categories_slugs = array( 'term1', 'term2' ); $posts = get_posts(array( 'post_type' => 'product', 'numberposts' => -1, 'fields' => 'ids', 'post_status' => 'publish', 'ignore_sticky_posts' => true, 'suppress_filters' => true, 'has_password' => false, 'no_found_rows' => 1, 'lang' => '', 'tax_query' => array(array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $categories_slugs, 'operator' => 'IN' )) )); if ( $posts ) { $posts_str = implode(',', $posts); $relevance = "( case when ID IN ( {$posts_str} ) then 800 else 0 end ) + "; $query['relevance'] = preg_replace( '/(SUM([sS]*?([sS]*?case[sS]*?end[sS]*?)[sS]*?+/i', '$0' . $relevance, $query['relevance'] ); } return $query; }
You can take the sorting of search results completely into your own hands. You can order rules based on different criteria like product date, product stock status, product quantity number, etc.
There are no limits on how you choose to order your search results. All this can be done via custom code snippets. Please read this guide that includes many examples of such ordering functions.