php – Wp Query : Order by distance lat,lon
I’m new to this forum, sorry in advance 🙂
I have a problem with my display of my posts
Let me explain ^^
I want to perform an orderby => ‘$distance’ and display the posts accordingly.
$lat2 and $lon2 are listed in wp.postmeta in meta_key and meta_value. Also $lat and $lon are well recovered without worrie.
I tried to put meta_key/value in my query, but it doesn’t work because my calculation is not done in my database.
object(WP_Query)[9785]
public 'query' =>
array (size=5)
'post_type' => string 'post' (length=4)
'lon' => string '43.282' (length=6)
'lat' => string '5.405' (length=5)
'orderby' => float 4844
'order' => string 'asc' (length=3)
I have a city filter which when clicked launches my function. I retrieve the radius that I set to 20.30 or 50 depending on the user’s select option
I created a post-view.php template which retrieves the found post well, my template is a loop if $rayon<50 and there are posts it shows me the template several times 🙂
function filter_city(){
$rayon = $_POST['rayon'];
$lon = htmlspecialchars($_POST["lon"]);
$lat = htmlspecialchars($_POST["lat"]);
$lat2 = get_lat(get_the_id());
$lon2 = get_lon(get_the_id());
$resultat = Misc::distance($lat, $lon, $lat2, $lon2, $miles = false);
$m = round($resultat);
$myposts = new WP_Query([
'post_type' => 'post',
'lon' => $lon,
'lat' => $lat,
'orderby' => $m,
'order' => 'asc'
]);
$response="";
if ($myposts->have_posts()) {
while ($myposts->have_posts()) : $myposts->the_post();
include('post-view.php');
endwhile;
} else {
$response .= include('false.php');
}
I’ve tried everything
Thank you in advance for your help
Leave an answer