Description
This filter allows you to change the options in the “Sort by” dropdown on Store Locator > Settings > Search (Search settings). The option you select there, together with the “Sort order” setting, sets the order of the search results.
Use it to add your own field as a sort option, for example a priority number that puts your main locations first.
These are the default options:
| Key | Label |
|---|---|
distance |
Distance (default) |
store |
Store Name |
address |
Address |
id |
ID |
city |
City |
zip |
Zip code |
state |
State |
country |
Country |
When “Search method” is set to “Name search (post / location name)”, no distance is calculated, so the Distance option isn’t shown in the dropdown.
Parameters
$options - (array) The sort options. The key is the name of a field in the location data, the value is the label shown in the "Sort by" dropdown.
Usage
This example adds a Priority option to the “Sort by” dropdown:
| add_filter( 'wpsl_search_order_options', 'custom_search_order_options' ); | |
| function custom_search_order_options( $options ) { | |
| // The key is the field name in the location data, the value the label in the "Sort by" dropdown. | |
| $options['priority'] = esc_html__( 'Priority', 'your-textdomain' ); | |
| return $options; | |
| } |
After adding the code, go to Store Locator > Settings > Search, select “Priority” under “Sort by”, pick the “Sort order”, and save the settings.
The key, priority in this example, has to be the name of a field in the location data, or the results stay in the order of the distance. The location data holds the default fields, like store, address and city, and every field you add under Store Locator > Settings > Fields Manager (Fields Manager settings). A Fields Manager field is added under the value in its Name column. So for this example, create a field with the name priority and fill in a number for your locations.
A field that you add with code needs to be in the location data too. Add it with wpsl_frontend_meta_fields or wpsl_store_meta. A value added with wpsl_store_data is too late, because that filter runs after the results are sorted.
How the sort works:
- The search itself doesn’t change. It still finds the nearest locations within the search radius, up to the maximum number of results. The sort then changes the order in which those locations are shown.
- Locations without a value for the field are shown after the ones with a value, for both sort orders.
- Numbers are sorted as numbers, so 9 comes before 10.
Changelog
| Version | Description |
|---|---|
| 3.0 | Introduced. |
Source File
This filter is located in /includes/core/functions/functions-settings.php.
Placement
The code needs to be placed in the functions.php file inside your active theme folder.