I have two custom fields on my posts with values that could be like
get_usp-custom-19 = 40.85150386578784
get_usp-custom-20 = 14.258907499999964
Then I receive a GET from a form with some values like this:
$lat = $_GET['usp-custom-19'];
$ln = $_GET['usp-custom-20'];
Now $lat
and $ln
have latitude
and a longitude
separated values and I create a query like:
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'relation' => 'AND',
array(
'key' => 'get_usp-custom-19',
'value' => $lat,
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'get_usp-custom-20',
'value' => $ln,
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
),
),
);
But I get wrong results as it isn't comparing in between, I also tried to set it type
as CHAR
or DECIMALS
but still, wrong results. Also I could get negative coords like -9.258907499999964
and I was reading about cons
and sen
or using abs()
, but I am getting very confused now
UPDATE
Here I try to create a radius
$lat = $_GET['usp-custom-19'];
$ln = $_GET['usp-custom-20'];
$args = get_posts(
array(
'post_type' => 'post',
'posts_per_page' => -1,
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$customCoords = usp_get_meta(false, 'usp-custom-90');
$arrayCoords = explode( ",", $customCoords );
$radiusLn = +$arrayCoords[0] + 10;
$radiusLat = +$arrayCoords[1] + 10;
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'relation' => 'AND',
array(
'key' => 'get_usp-custom-19',
'value' => array($ln, $radiusLn),
'compare' => '>='
),
array(
'key' => 'get_usp-custom-20',
'value' => array($lat, $radiusLat),
'compare' => '<='
),
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
the_title();
}
}
}
}