I have a custom post type “Store” and store address saved in postmeta table as geo_latitude, geo_longitude, geo_public, geo_address as WordPress recommend. Then we have all registered user latitude and longitude and stored in user meta. Now I want to get nearest 5 or 10 store id (basically postid) from the database using Haversine formula to calculate the radius.
I found a good thread in StackOverflow but there latitude and latitude as table row but my case latitude and latitude is row value (meta value). A sample database structure has attached.
This will do it (no pivot table) with self joins:
gives you a table with post_id, lat, lng, address