<?php
// For a Proximity facet, index an ACF Google Maps field of a post linked by an ACF Post Object field.
// The Proximity facet's data source can be set to anything as long as that value is not empty. E.g. 'Post Type'.
// The 'Return Format' of the Post Object field does not matter: it works with both 'Post Object' and 'Post ID'.
// See: https://facetwp.com/help-center/developers/hooks/indexing-hooks/facetwp_index_row/#index-values-in-a-related-custom-field
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'my_proximity_facet' == $params['facet_name'] ) { // Change "my_proximity_facet" to the name of your Proximity facet
$post_id = $params['post_id'];
// Get the Post Object field value
$location_id = get_field( 'my_acf_post_object field', $post_id ); // Change "my_acf_post_object field" to the name of your ACF Post Object field
$location = '';
if ( $location_id ) { // Check if the Post Object field has a value
// If we have a Post Object field value, use it to get the Google Maps field value
$location = get_field( 'my_acf_googlemapfield', $location_id ); // Change "my_acf_googlemapfield" to the name of your ACF Google Maps field
}
// If there is a location, index it, otherwise skip this post by setting facet_value to ''.
$params['facet_value'] = empty( $location ) ? '' : $location['lat'];
$params['facet_display_value'] = empty( $location ) ? '' : $location['lng'];
// Optionally replicate the extra checks that Proximity facets normally have:
// 1. Make sure lat and lng are valid floats
$params['facet_value'] = $params['facet_value'] == (float)$params['facet_value'] ? (float)$params['facet_value'] : '';
$params['facet_display_value'] = $params['facet_display_value'] == (float)$params['facet_display_value'] ? (float)$params['facet_display_value'] : '';
// 2. Check for a valid range of lat and lng
if ( '' == $params['facet_value'] || '' == $params['facet_display_value'] || 90 < abs( $params['facet_value'] ) || 180 < abs( $params['facet_display_value'] ) ) {
$params['facet_value'] = ''; // don't index
}
}
return $params;
}, 10, 2 );