Last active
June 29, 2023 14:14
-
-
Save daltonrooney/74b55f4ee3ec17c7ba00 to your computer and use it in GitHub Desktop.
Store locator with Advanced Custom Fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function mbn_get_stores_by_location( $zip, $radius ) { | |
global $wpdb; | |
$radius = intval( $radius ); | |
// we first need to get the source coordinates | |
$sql = "SELECT `latitude`, `longitude` FROM `wp_zip_codes` WHERE `zipcode` = '%s'"; | |
$coords = $wpdb->get_row( $wpdb->prepare( $sql, $zip ) ); | |
// now we'll get the other ZIP codes within the radius, ordered by distance | |
$sql = "SELECT wp_zip_codes.zipcode, ( 3959 * acos( cos( radians( $coords->latitude ) ) * cos( radians( wp_zip_codes.latitude ) ) * cos( radians( wp_zip_codes.longitude ) - radians( $coords->longitude ) ) + sin( radians( $coords->latitude ) ) * sin( radians( wp_zip_codes.latitude ) ) ) ) AS distance FROM wp_zip_codes HAVING distance <= $radius OR distance IS NULL ORDER BY distance"; | |
$nearby_zips = $wpdb->get_results( $sql ); | |
// Store the zips in order to build the meta query | |
$target_zips = array(); | |
foreach ($nearby_zips as $nearby_zip) : | |
array_push($target_zips, $nearby_zip->zipcode); | |
endforeach; | |
// we're going to store the results as we go | |
$store_results = array(); | |
if ( count( $target_zips > 0 ) ) : | |
$args = array( | |
'post_type' => 'retail-store', | |
'posts_per_page' => 24, | |
'meta_query' => array( | |
array( | |
'key' => 'zip', | |
'value' => $target_zips, | |
'compare' => 'IN', | |
), | |
), | |
); | |
$store_query = new WP_Query($args); | |
if ( $store_query->have_posts() ) : while ( $store_query->have_posts() ) : | |
$store_query->the_post(); | |
$store_data = array(); | |
$store_data['id'] = get_the_ID(); | |
$store_data['name'] = get_the_title(); | |
$store_data['street_address'] = get_field('street_address'); | |
$store_data['street_address_2'] = get_field('street_address'); | |
$store_data['city'] = get_field('city'); | |
$store_data['state'] = get_field('state'); | |
$store_data['zip'] = get_field('zip'); | |
$store_data['telephone'] = get_field('telephone'); | |
$store_data['website'] = get_field('website'); | |
foreach ($nearby_zips as $nearby_zip) : | |
if( $store_data['zip'] == $nearby_zip->zipcode ) : | |
$store_data['distance'] = intval( $nearby_zip->distance ); | |
endif; | |
endforeach; | |
array_push( $store_results, $store_data ); | |
unset( $store_data ); | |
endwhile; endif; | |
usort( $store_results, "mbn_cmp" ); | |
endif; | |
return $store_results; | |
wp_reset_postdata(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function rg_store_publish() { | |
global $post; | |
$lat = get_post_meta($post->ID, 'lat', true); | |
if ( empty($lat) ) : | |
$address = urlencode(get_field('street_address') ) . '+' . urlencode(get_field('city') ) . ',+' . urlencode(get_field('state') ) . ',+' . urlencode(get_field('zip') ); | |
$geocode_request = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . $address; | |
$response = wp_remote_get( $geocode_request ); | |
$obj = json_decode($response['body'], true); | |
$location = $obj["results"][0]['geometry']['location']; | |
update_post_meta($post->ID, 'lat', $location['lat']); | |
update_post_meta($post->ID, 'lng', $location['lng']); | |
endif; | |
} | |
add_action( 'publish_retail-store', 'rg_store_publish', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on Jonathan Christopher's post here: https://mondaybynoon.com/store-locator-wordpress-pods/