wordpress get posts within radius defiend by user wordpress get posts within radius defiend by user wordpress wordpress

wordpress get posts within radius defiend by user


Here is my final query. Does exactly what i need it to do. I have created a custom table in my database to store lat and lng info from posts VS adding them to the post meta table so i could join the 2 and get the distance from the post to the user. Any question let me know :)

$querystr = '            SELECT *,            (((acos(sin(('.$userLat.'*pi()/180)) * sin((lat*pi()/180))+cos(('.$userLat.'*pi()/180)) * cos((lat*pi()/180)) * cos((('.$userLng.'- lng)*pi()/180))))*180/pi())*60*1.1515) as distance            FROM wp_posts, tbl_post_lat_lng            WHERE wp_posts.ID = tbl_post_lat_lng.post_id            AND wp_posts.post_status = "publish"            AND wp_posts.post_type = "post"            AND distance <= '.$userRadius.'            ORDER BY wp_posts.post_date DESC            LIMIT 5         ';


is it as simple as just not specifying a number of posts to be returned, and then just skipping the loop iteration if the post doesn't match your geolocation requirement?

But I guess this could perform badly if there's a lot of posts...

This looks like how to do it in-query: Codex: Displaying Posts Using a Custom Select Query


If anybody needs to filter by distance radius then you may check this post. https://gschoppe.com/wordpress/location-searches/