Overview
Plugin search results ordered based on products relevance. N products with higher relevance will be displayed as search results. This number of displayed search results can be set from the plugin settings page.
It is possible to add a second ordering rule for those N products that are chosen to be shown as search results. Below we will describe some examples of such order rules.
Order by product name alphabetically
Use the following code snippet to order products search results by title in alphabetical order from A to Z.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids' );
function my_aws_search_results_products_ids( $products ) {
usort($products, function ($item1, $item2) {
$a = get_the_title($item1);
$b = get_the_title($item2);
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
});
return $products;
}
Order by product name length
Sort products by product title length from shortest to longest.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids' );
function my_aws_search_results_products_ids( $products ) {
usort($products, function ($item1, $item2) {
$a = strlen(get_the_title($item1));
$b = strlen(get_the_title($item2));
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
});
return $products;
}
Order by product price
Sort products by pricing from lowest to highest.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids' );
function my_aws_search_results_products_ids( $products ) {
usort($products, function ($a, $b) {
$price_a = get_post_meta( $a, '_price', true );
$price_b = get_post_meta( $b, '_price', true );
$price_a = intval( $price_a ) * 100;
$price_b = intval( $price_b ) * 100;
if ( ! is_numeric( $price_a ) || ! is_numeric( $price_b ) ) {
return 0;
}
if ($price_a == $price_b) {
return 0;
}
return ($price_a < $price_b) ? -1 : 1;
});
return $products;
}
Order by product stock status
Lets order products by stock statuses. In stock products first and Out of stock products in the end.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids' );
function my_aws_search_results_products_ids( $products ) {
usort($products, function ($a, $b) {
$status_a = get_post_meta( $a, '_stock_status', true );
$status_b = get_post_meta( $b, '_stock_status', true );
if ( ! $status_a || ! $status_b ) {
return 0;
}
if ( 'instock' === $status_a ) {
return -1;
}
if ( 'instock' === $status_b ) {
return 1;
}
return 0;
});
return $products;
}
Order by product rating
Sort search results by products ratings from highest to lowest.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids' );
function my_aws_search_results_products_ids( $products ) {
usort($products, function ($a, $b) {
$rating_a = get_post_meta( $a, '_wc_average_rating', true );
$rating_b = get_post_meta( $b, '_wc_average_rating', true );
if ( ! $rating_a || ! $rating_b ) {
return 0;
}
$rating_a = intval( $rating_a * 100 );
$rating_b = intval( $rating_b * 100 );
if ($rating_a == $rating_b) {
return 0;
}
return ($rating_a < $rating_b) ? -1 : 1;
});
return $products;
}
Order by product menu order
Product menu order can be set from the product Advanced tab -> Menu order option. Lets order products' search results by this option from lowest to highest.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids' );
function my_aws_search_results_products_ids( $products ) {
usort($products, function ($a, $b) {
$order_a = get_post_field( 'menu_order', $a);
$order_b = get_post_field( 'menu_order', $b);
if ($order_a === $order_b) {
return 0;
}
return ($order_a < $order_b) ? -1 : 1;
});
return $products;
}
Order by product SKU
If, for some reason, you need to order your products by SKU number, then you can use the following code snippet to order products from lowest to highest SKU value.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids' );
function my_aws_search_results_products_ids( $products ) {
usort($products, function ($a, $b) {
$sku_a = get_post_meta( $a, '_sku', true );
$sku_b = get_post_meta( $b, '_sku', true );
if ( ! $sku_a) {
$parent_id_a = wp_get_post_parent_id( $a );
if ( $parent_id_a ) {
$sku_a = get_post_meta( $parent_id_a, '_sku', true );
}
}
if ( ! $sku_b) {
$parent_id_b = wp_get_post_parent_id( $b );
if ( $parent_id_b ) {
$sku_b = get_post_meta( $parent_id_b, '_sku', true );
}
}
if ( ! $sku_a && ! $sku_b ) {
return 0;
}
if ( ! $sku_a ) {
return 1;
}
if ( ! $sku_b ) {
return -1;
}
if ($sku_a == $sku_b) {
return 0;
}
return ($sku_a < $sku_b) ? 1 : -1;
});
return $products;
}
Order by product sales number
Lets order our search results by the total number of product sales from highest to lowest.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids' );
function my_aws_search_results_products_ids( $products ) {
usort($products, function ($a, $b) {
$order_a = get_post_field( 'total_sales', $a, true);
$order_b = get_post_field( 'total_sales', $b, true);
if ( ! $order_a) {
$parent_id_a = wp_get_post_parent_id( $a );
if ( $parent_id_a ) {
$order_a = get_post_meta( $parent_id_a, 'total_sales', true );
}
}
if ( ! $order_b) {
$parent_id_b = wp_get_post_parent_id( $b );
if ( $parent_id_b ) {
$order_b = get_post_meta( $parent_id_b, 'total_sales', true );
}
}
if ($order_a === $order_b) {
return 0;
}
return ($order_a < $order_b) ? 1 : -1;
});
return $products;
}
Order by product creation date
Sort products by date of creation. Newest first.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids' );
function my_aws_search_results_products_ids( $products ) {
usort($products, function ($a, $b) {
$post_a = get_post( $a );
$post_b = get_post( $b );
if ( ! $post_a || ! $post_b ) {
return 0;
}
$date_a = strtotime( $post_a->post_date );
$date_b = strtotime( $post_b->post_date );
if ($date_a == $date_b) {
return 0;
}
return ($date_a < $date_b) ? 1 : -1;
});
return $products;
}
Order by product custom field
For example we have a product custom field named wc_s_num that contains some integer value and we want to sort search results by this value from lowest to highest.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids' );
function my_aws_search_results_products_ids( $products ) {
usort($products, function ($a, $b) {
$field_name = 'wc_s_num';
$custom_fields_a = get_post_custom( $a );
$custom_fields_b = get_post_custom( $b );
if ( ! $custom_fields_a || ! isset( $custom_fields_a[$field_name] ) ) {
return 1;
}
if ( ! $custom_fields_b || ! isset( $custom_fields_b[$field_name] ) ) {
return -1;
}
$custom_fields_a_val = intval( $custom_fields_a[$field_name][0] );
$custom_fields_b_val = intval( $custom_fields_b[$field_name][0] );
if ($custom_fields_a_val == $custom_fields_b_val) {
return 0;
}
return ($custom_fields_a_val < $custom_fields_b_val) ? -1 : 1;
});
return $products;
}
Order by product stock status + price
Lets order products by stock statuses. In stock products first and Out of stock products in the end. And also all those In stock we will order by price from lowest to highest.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids' );
function my_aws_search_results_products_ids( $products ) {
usort($products, function ($a, $b) {
$status_a = get_post_meta( $a, '_stock_status', true );
$status_b = get_post_meta( $b, '_stock_status', true );
if ( ! $status_a || ! $status_b ) {
return 0;
}
if ( $status_a === 'instock' && $status_b === 'instock' ) {
$price_a = get_post_meta( $a, '_price', true );
$price_b = get_post_meta( $b, '_price', true );
$price_a = intval( $price_a ) * 100;
$price_b = intval( $price_b ) * 100;
if ( ! is_numeric( $price_a ) || ! is_numeric( $price_b ) ) {
return 0;
}
if ($price_a == $price_b) {
return 0;
}
return ($price_a < $price_b) ? -1 : 1;
}
if ( 'instock' === $status_a ) {
return -1;
}
if ( 'instock' === $status_b ) {
return 1;
}
return 0;
});
return $products;
}
Order by product category
For example we have a product category called Featured and we want to place products that have this category at the top of search results and all other products after.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids' );
function my_aws_search_results_products_ids( $products ) {
usort($products, function ($item1, $item2) {
$category_name = 'Featured';
$terms1 = wp_get_object_terms( $item1, 'product_cat' );
$terms2 = wp_get_object_terms( $item2, 'product_cat' );
if ( is_wp_error( $terms1 ) || empty( $terms1 ) || is_wp_error( $terms2 ) || empty( $terms2 ) ) {
return 0;
}
foreach( $terms1 as $terms1_arr ) {
if ( $terms1_arr->name === $category_name ) {
return -1;
}
}
foreach( $terms2 as $terms2_arr ) {
if ( $terms2_arr->name === $category_name ) {
return 1;
}
}
return 0;
});
return $products;
}
Order by product featured status
We can order products depending on their featured status. So let's show featured products first and then all other.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids' );
function my_aws_search_results_products_ids( $products ) {
usort($products, function ($item1, $item2) {
$product1 = wc_get_product( $item1 );
$product2 = wc_get_product( $item2 );
if ( $product1->is_featured() ) {
return -1;
}
if ( $product2->is_featured() ) {
return 1;
}
return 0;
});
return $products;
}
Order by product image
Order by product main image - display products with images first.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids' );
function my_aws_search_results_products_ids( $products ) {
usort($products, function ($a, $b) {
$image_a = get_post_meta( $a, '_thumbnail_id', true );
$image_b = get_post_meta( $b, '_thumbnail_id', true );
if ( ! $image_a) {
$parent_id_a = wp_get_post_parent_id( $a );
if ( $parent_id_a ) {
$image_a = get_post_meta( $parent_id_a, '_thumbnail_id', true );
}
}
if ( ! $image_b ) {
$parent_id_b = wp_get_post_parent_id( $b );
if ( $parent_id_b ) {
$image_b = get_post_meta( $parent_id_b, '_thumbnail_id', true );
}
}
if ($image_a == $image_b) {
return 0;
}
return ($image_a < $image_b) ? 1 : -1;
});
return $products;
}
Order users by custom fields
For example, we are enabling users archive pages search and want to display the results by value from some user custom field hits. To achieve this just use the following code snippet:
add_filter( 'aws_search_users_results', 'my_aws_search_users_results' );
function my_aws_search_users_results( $users ) {
usort($users, function ($item1, $item2) {
$field_name = 'hits';
$custom_field_a = get_user_meta( $item1[0]['id'], $field_name, true );
$custom_field_b = get_user_meta( $item2[0]['id'], $field_name, true );
if ( ! $custom_field_a ) {
return 1;
}
if ( ! $custom_field_b ) {
return -1;
}
if ($custom_field_a == $custom_field_b) {
return 0;
}
return ($custom_field_a < $custom_field_b) ? -1 : 1;
});
return $users;
}
Order categories results by count
If you are using products categories search and want to display product categories results ordered by number of products inside each of that category then you can use the following code snippet:
add_filter( 'aws_search_tax_results', 'my_aws_search_tax_results' );
function my_aws_search_tax_results( $results ) {
if ( isset( $results['product_cat'] ) ) {
usort($results['product_cat'], function ($a, $b) {
return (int)$b['count'] - (int)$a['count'];
});
}
return $results;
}