Description
This filter allows you to modify the search results data just before they are sent to the store locator.
The filter gets the full list of locations that the search found. Each location is an array with keys like id, store, address, city, lat, lng, phone and url, plus the fields you created in the Fields Manager. Return the list in the same format.
If you only need to change the data of each location on its own, the wpsl_store_meta filter is often easier. Use this filter when you need the whole list, for example to change the order of the results.
Usage
This example moves the stores in the store category with the slug flagship to the top of the search results. Within each group, the stores keep their original order.
| add_filter( 'wpsl_store_data', 'custom_store_data' ); | |
| function custom_store_data( $store_data ) { | |
| $featured = array(); | |
| $other = array(); | |
| // Move the stores in the category with the slug "flagship" to the top of the results. | |
| foreach ( $store_data as $store ) { | |
| if ( has_term( 'flagship', 'wpsl_store_category', $store['id'] ) ) { | |
| $featured[] = $store; | |
| } else { | |
| $other[] = $store; | |
| } | |
| } | |
| return array_merge( $featured, $other ); | |
| } |
Some things to keep in mind:
- The filter doesn’t run when the search finds no locations.
- Online stores, from the “Include online stores?” option in Store Locator > Settings > Store Editor, are added to the results after this filter runs. Your code never sees them, and they always appear first.
- With Mapbox, the results are converted to GeoJSON after this filter runs. Your code still gets the normal list. Keep the
latandlngkeys, because a location without them isn’t shown on the map.
Changelog
| Version | Description |
|---|---|
| 3.0 | Moved to includes/frontend/search/class-search.php. It also runs for searches that don't use coordinates, like a category search, in includes/frontend/search/class-types.php. Online stores are added after the filter. With Mapbox, the results are converted to GeoJSON after the filter. |
| 2.2.0 | Introduced. |
Source File
This filter is located in /includes/frontend/search/class-search.php and /includes/frontend/search/class-types.php.
Placement
The code needs to be placed in the functions.php file inside your active theme folder.