Description
This filter allows you to change the component restrictions that are sent with each Google geocode request made from the search field. You can restrict the results by administrativeArea, country, locality, postalCode and route.
If you, for example, search for Springfield, the first result Google returns may be the Springfield in Massachusetts. That’s fine if your stores are in Massachusetts. But if they’re in Illinois, your visitors only see a “No results found” message. Restricting the results to Illinois fixes this.
The filter only runs in these two cases:
- Enable autocomplete? is checked on Store Locator > Settings > Search, and the Autocomplete source is “Places Autocomplete Service (legacy)”. The array is empty, or holds
postalCodewhen Force zip code only search is checked. - The search is restricted to exactly one country. That’s either Country filtering set to “Restrict results (hard)” with one country selected under Restrict the search results to one or more countries on Store Locator > Settings > API, or a shortcode with one country, like
[wpsl country="us"]. The array then holdscountrywith the two letter country code.
When both are true, the filter runs twice, and the second time it gets the result of the first run plus the country.
If you use the “Autocomplete Data API (new)” source, or no autocomplete, then restrict the search to your one country first. That makes the filter run. For example, restrict it to the United States, and use the filter to narrow the results down to Illinois.
To restrict the search to more than one country, use the settings described in Restrict the Search Results to Different Countries. Google’s component restrictions only accept a single country.
Usage
This example restricts the geocode results to Illinois.
| add_filter( 'wpsl_geocode_components', 'custom_geocode_components' ); | |
| function custom_geocode_components( $geocode_components ) { | |
| $geocode_components['administrativeArea'] = 'Illinois'; | |
| return $geocode_components; | |
| } |
Changelog
| Version | Description |
|---|---|
| 3.0 | Moved to includes/frontend/assets/class-resources.php. Google Maps only. The filter now runs when the legacy Places Autocomplete Service is used, or when the search is restricted to exactly one country (API tab "Country filtering" set to "Restrict results (hard)", or the country shortcode attribute). This replaces the map region and "Restrict the geocoding results to the selected map region?" option from 2.x. |
| 2.1 | Introduced. |
Source File
This filter is located in /includes/frontend/assets/class-resources.php.
Placement
The code needs to be placed in the functions.php file inside your active theme folder.