GET FREE VERSION GET THE PRO VERSION

Ordering Search Results

Learn how to set custom ordering rules for the plugin search results.

In this article

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.

Custom orders will apply for both live search results and for products inside the search results page.

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', 'my_aws_search_results_products' );
function my_aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
        $a = strtolower( $item1['title'] );
        $b = strtolower( $item2['title'] );
        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', 'aws_search_results_products' );
function aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
        $a = strlen( $item1['title'] );
        $b = strlen( $item2['title'] );
        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', 'my_aws_search_results_products' );
function my_aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
        $a = intval( $item1['f_price'] * 100 );
        $b = intval( $item2['f_price'] * 100 );
        if ($a == $b) {
            return 0;
        }
        return ($a < $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', 'my_aws_search_results_products' );
function my_aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
        $product1 = wc_get_product( $item1['id'] );
        $product2 = wc_get_product( $item2['id'] );
       if ( 'instock' === $product1->get_stock_status() ) {
            return -1;
        }
        if ( 'instock' === $product2->get_stock_status() ) {
            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', 'my_aws_search_results_products' );
function my_aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
        $a_rating = strtolower( $item1['f_rating'] );
        $b_rating = strtolower( $item2['f_rating'] );
        if ($a_rating == $b_rating ) { return 0; }
        return ($a_rating < $b_rating) ? -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', 'my_aws_search_results_products' );
function my_aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
        $product1 = wc_get_product( $item1['id'] );
        $product2 = wc_get_product( $item2['id'] );
        $a = $product1->get_menu_order();
        $b = $product2->get_menu_order();
        if ($a == $b) {
            return 0;
        }
        return ($a < $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', 'my_aws_search_results_products' );
function my_aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
        $product1 = wc_get_product( $item1['id'] );
        $product2 = wc_get_product( $item2['id'] );
        $a = $product1->get_sku();
        $b = $product2->get_sku();
        if ($a == $b) {
            return 0;
        }
        return ($a < $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', 'my_aws_search_results_products' );
function my_aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
        $product1 = wc_get_product( $item1['id'] );
        $product2 = wc_get_product( $item2['id'] );
        $a = $product1->get_total_sales();
        $b = $product2->get_total_sales();
        if ($a == $b) {
            return 0;
        }
        return ($a < $b) ? 1 : -1;
    });
    return $products;
}

Order by product creation date

Sort products by date of creation. Newest first.

add_filter( 'aws_search_results_products', 'my_aws_search_results_products' );
function my_aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
        $a = strtotime( $item1['post_data']->post_date );
        $b = strtotime( $item2['post_data']->post_date );
        if ($a == $b) {
            return 0;
        }
        return ($a < $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', 'my_aws_search_results_products' );
function my_aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
        $field_name = 'wc_s_num';
        $custom_fields_a = get_post_custom( $item1['post_data']->ID );
        $custom_fields_b = get_post_custom( $item2['post_data']->ID );
        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', 'my_aws_search_results_products' );
function my_aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
        $product1 = wc_get_product( $item1['id'] );
        $product2 = wc_get_product( $item2['id'] );
        if ( $product1->get_stock_status() === 'instock' && $product2->get_stock_status() === 'instock' ) {
            $a_price = intval( $item1['f_price'] * 100 );
            $b_price= intval( $item2['f_price'] * 100 );
            if ($a_price == $b_price) {
                return 0;
            }
            return ($a_price < $b_price) ? -1 : 1;
        }
        if ( 'outofstock' !== $product1->get_stock_status() ) {
            return -1;
        }
        elseif ( 'outofstock' !== $product2->get_stock_status() ) {
            return 1;
        }

    });
    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', 'my_aws_search_results_products' );
function my_aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
        $category_name = 'Featured';
        $terms1 = wp_get_object_terms( $item1['parent_id'], 'product_cat' );
        $terms2 = wp_get_object_terms( $item2['parent_id'], '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;
}

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', 'my_aws_search_results_products' );
function my_aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
        $product1 = wc_get_product( $item1['id'] );
        $product2 = wc_get_product( $item2['id'] );
        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', 'my_aws_search_results_products' );
function my_aws_search_results_products( $products ) {
    usort($products, function ($item1, $item2) {
        $product1 = wc_get_product( $item1['id'] );
        $product2 = wc_get_product( $item2['id'] );
        $a = !! $product1->get_image_id();
        $b = !! $product2->get_image_id();
        if ($a == $b) {
            return 0;
        }
        return ($a < $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;
}