Change the Order of the Search Results

By default, the search results are always ordered by distance, but this can be easily changed by using the wpsl_store_data filter.

The code example below shows how to sort the results alphabetically by store name:

add_filter( 'wpsl_store_data', 'custom_result_sort' );

function custom_result_sort( $store_meta ) {
    
    $custom_sort = array();
    
    foreach ( $store_meta as $key => $row ) {
        $custom_sort[$key] = $row['store'];
    }

    array_multisort( $custom_sort, SORT_ASC, SORT_REGULAR, $store_meta );
    
    return $store_meta;
}

Place the above code in the functions.php inside your active the folder, and make sure to flush the WPSL transient cache on the settings page before reloading the store locator. Once you have done that the search results should be ordered by store name instead of distance.

You can reverse the sorting by changing SORT_ASC to SORT_DESC on line 11.

If you want to sort the search results by another field, you have to change the key that’s used in the $row var on line 8, and set it to the name of the location data you want to base the sorting on.

The code example below sorts the search results by city:

$custom_sort[$key] = $row['city'];

You can use any data field to sort the search results. So sorting the search results on, for example, the id, address, city, zip, or a custom field is also possible.