Skip to content

Instantly share code, notes, and snippets.

@ChrisBuchholz
Created November 12, 2013 21:10
Show Gist options
  • Save ChrisBuchholz/7438767 to your computer and use it in GitHub Desktop.
Save ChrisBuchholz/7438767 to your computer and use it in GitHub Desktop.
<?php
function get_cars() {
$ne = $_REQUEST['northeast'];
$sw = $_REQUEST['southwest'];
$args = array(
'post_type' => 'car',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'locationlat',
'value' => array($sw[0], $ne[0]),
'compare' => 'BETWEEN'
),
array(
'key' => 'locationlng',
'value' => array($sw[1], $ne[1]),
'compare' => 'BETWEEN'
),
)
);
$query = new WP_Query($args);
$cars_inside_bounds = [];
$carIds = [];
while ($query->have_posts()) {
$query->the_post();
$car_info = array(get_field(CAR::LOCATIONLAT), get_field(CAR::LOCATIONLNG)); //makes location string into lat and lng array
$carImages = get_field(CAR::GALLERY);
array_push($car_info, get_the_ID());
array_push($car_info, get_permalink());
array_push($car_info, get_field(CAR::MODEL));
array_push($car_info, get_field(CAR::RENT_PERDAY));
array_push($car_info, $carImages[0]['sizes']['large']);
array_push($cars_inside_bounds, $car_info);
}
print_r(json_encode($cars_inside_bounds));
die();
}
function get_cars_if() {
$ne = $_REQUEST['northeast'];
$sw = $_REQUEST['southwest'];
$args = array(
'post_type' => 'car',
'post_status' => 'publish',
);
$query = new WP_Query($args);
$cars_inside_bounds = [];
$carIds = [];
while ($query->have_posts()) {
$query->the_post();
$car_info = array(get_field(CAR::LOCATIONLAT), get_field(CAR::LOCATIONLNG)); //makes location string into lat and lng array
$lat = $car_info[0];
$lng = $car_info[1];
if ($lat >= $sw[0] && $lat <= $ne[0] && $lng >= $sw[1] && $lng <= $ne[1]) {
$carImages = get_field(CAR::GALLERY);
array_push($car_info, get_the_ID());
array_push($car_info, get_permalink());
array_push($car_info, get_field(CAR::MODEL));
array_push($car_info, get_field(CAR::RENT_PERDAY));
array_push($car_info, $carImages[0]['sizes']['large']);
array_push($cars_inside_bounds, $car_info);
}
}
print_r(json_encode($cars_inside_bounds));
die();
}
add_action('wp_ajax_nopriv_get_cars', 'get_cars');
add_action('wp_ajax_get_cars', 'get_cars');
add_action('wp_ajax_nopriv_get_cars_if', 'get_cars_if');
add_action('wp_ajax_get_cars_if', 'get_cars_if');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment