Home › Forums › Bug Reports › Google Maps field normalization › Reply To: Google Maps field normalization
Workaround for anyone experiencing the same problem: hook to the ‘acf/save_post’ action and use update_post_meta() to duplicate the latitude and longitude data to hidden meta fields, enabling a custom query to be crafted to return posts by distance:
`function location_search($latitude, $longitude, $maxdistance = 5) {
global $wpdb;
$query = “SELECT wp_posts.post_title as post_title,
wp_posts.ID as post_id,
latitude.meta_value as latitude,
longitude.meta_value as longitude,
( 3959 * acos(
cos( radians( $latitude ) ) *
cos( radians( CONVERT( latitude.meta_value, DECIMAL( 10, 6 ) ) ) ) *
cos( radians( CONVERT( longitude.meta_value, DECIMAL( 10, 6 ) ) ) – radians( $longitude ) ) +
sin( radians( $latitude ) ) * sin( radians( CONVERT( latitude.meta_value, DECIMAL( 10, 6 ) ) ) )
) ) AS distance
FROM wp_postmeta as latitude, wp_postmeta as longitude,
wp_posts
WHERE (wp_posts.ID = latitude.post_id
AND latitude.meta_key = ‘_latitude’ )
AND (wp_posts.ID = longitude.post_id
AND longitude.meta_key = ‘_longitude’ )
HAVING distance < $maxdistance
ORDER BY distance ASC”;
return $query;
}`
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.