I will try to clarify.
The problem is that the distance from a User to any post is not constant. Distances must be calculated using the current User's location and the current post location. Distance will be different for each User, for each post. This means that you will not store distances in the database - they are calculated on-the-fly. There is no way to directly sort the results by distance, so we need a different sorting method.
Assume your algorithm to determine distance is very simple:
distance = (post lat + post long) / (userlat + userlong)
Your real algorithm is much more complex but will include these variables:
post latitude
post longitude
user latitude
user longitude
In order to sort a list of results by distance, you must calculate the distance for each result and compare the distances of all the results against each other. That means you must calculate each distance using the proper post latitudes and post longitudes. For example:
distance to post 1 = (post 1 lat + post 1 long) / (userlat + userlong)
distance to post 2 = (post 2 lat + post 2 long) / (userlat + userlong)
distance to post 3 = (post 3 lat + post 3 long) / (userlat + userlong)
distance to post 4 = (post 4 lat + post 4 long) / (userlat + userlong)
...and so on for all posts...
To sort a list by calculation in PHP, you need to use usort. The usort callback function compares each distance with each other distance by repeatedly comparing only two distances at a time using the algorithm you provide, until all the results have been sorted. Here is a sample of the usort function logic:
First comparison:
distance 1 = (post 1 lat + post 1 long) / (userlat + userlong)
distance 2 = (post 2 lat + post 2 long) / (userlat + userlong)
is distance 1 greater than distance 2?
if so, distance 1 is first. if not, distance 2 is first.
Second comparison
distance 2 = (post 2 lat + post 2 long) / (userlat + userlong)
distance 3 = (post 3 lat + post 3 long) / (userlat + userlong)
is distance 2 greater than distance 3?
If so, distance 2 is first. if not, distance 3 is first.
Third comparison
distance 3 = (post 3 lat + post 3 long) / (userlat + userlong)
distance 4 = (post 4 lat + post 4 long) / (userlat + userlong)
is distance 3 greater than distance 4?
If so distance 3 is first. If not, distance 4 is first.
... and so on until all the results have been compared with all the other results and the sort order has been determined. This is how usort compares a set of results using a calculation. So you must provide an algorithm to calculate distance to post "a" and to calculate distance to post "b". Your algorithm must support variables - different latitude and longitude for each post. In my code, these are represented by the variables $along, $alat, $blong, $blat. Our simple algorithm becomes:
$along = get_post_meta($a->ID, 'wpcf_longitude', true);
$alat = get_post_meta($a->ID, 'wpcf_latitude', true);
$blong = get_post_meta($b->ID, 'wpcf_longitude', true);
$blat = get_post_meta($b->ID, 'wpcf_latitude', true);
$user_long = ?; // not sure how you get this
$user_lat = ?; // not sure how you get this
$res = 0;
$dista = ($alat + $along) / ($user_lat + $user_long); // replace this with the correct algorithm using $alat and $along
$distb = ($blat + $blong) / ($user_lat + $user_long); // replace this with the correct algorithm using $blat and $blong
if ( $dista < $distb ) {
$res = 1;
}
return $res;