facetwp pre-select for proxmity from an ACF map field

djrmom

{{gist file=”custom-hooks.php” lang=”php”}}
<?php
/**
* Pre-select facet choices in FacetWP.
* ‘map_field_name’ needs to be changed to the name of your ACF google map field
* ‘proximity_facet_name’ needs to be changed to the name of your proximity facet field name
* get_page_by_path function needs additional parameters if using to get the ID of a post type other
* than “page” post type see https://developer.wordpress.org/reference/functions/get_page_by_path/
*/
add_filter( ‘facetwp_preload_url_vars’, function( $url_vars ) {
$page = get_page_by_path( FWP()->helper->get_uri() );
$location = get_field( ‘map_field_name’, $page->ID );
if ( !empty( $location ) ) {
if ( empty( $url_vars[‘proximity_facet_name’] ) ) {
// latitude, longitutde, radius, label
$url_vars[‘proximity_facet_name’] = array( $location[‘lat’], $location[‘lng’], ’10’, urlencode( $location[‘address’] ) ); // change 10 to whatever radius value you want
}
}
return $url_vars;
} );
{{/gist}}