A bit of a complicated problem here, but I figured I'd see if I could get any help.

My logic is this:

WordPress custom post query > for each of the results, factor in distance (calculated separately), based on a conditional push the result to an array ($results) > Sort results > for each of the results, display proper WP data.

I got everything working correctly up until the last part.

// initial wordpress query
$args = array(
    'post_type' => 'team-members',
    'posts_per_page' => -1,
    'meta_query' => array(
            'key' => 'specialties',
            'value' => $_GET['specialization'],
            'compare' => 'LIKE'
$loop = new WP_Query( $args );
$posts = $loop->get_posts();

// create array for results
$results = array();

foreach ($posts as $key => $value) {

    $result_lat = get_post_meta( $value->ID, 'latitude', true );
    $result_long = get_post_meta( $value->ID, 'longitude', true );
    $distance = round(calc_distance($input_lat, $input_lng, $result_lat, $result_long, "M"));

    // add item to results if within distance
    if ($distance < $_GET['within']) {
        $results[$key]['values'] = $value;  
        $results[$key]['distance'] = $distance; // add distance to array

// sort results by distance
usort($results, function($a, $b) { return $a["distance"] - $b["distance"]; });

foreach ($results as $key => $value) {
    echo get_the_ID();

In the case of get_the_ID(), it's not getting the correct post ID. Instead, it's just going back to the original query.

If I do a var_dump($results) before the last loop, I get an array that's structured like:

array(3) {
[0]=> array(2) {
    ["values"]=> object(WP_Post)#659 (24) {
        ["ID"]=> int(300)
        ["post_author"]=> string(1) "1"

I need to set WP_Post to be at the correct array for each of these items, but I cannot figure out how to do that.

  • You should sanitise and escape the $_GET values to prevent injection attacks
    – Tom J Nowell
    Commented Jul 6, 2015 at 20:54
  • I'm getting the idea this is very ineffecient and can be made more effecient with the proper adjustments. Commented Jul 7, 2015 at 11:22

1 Answer 1


get_the_ID gives you the ID of the current post, but at no point during that loop do you change what the current post is. It's always the last post processed in the previous loop because that's the last time you called the_post(). This is why you always get the same ID.

Instead, store the ID:

$results[$key]['ID'] = $value->ID;

Then echo that instead:

foreach ($results as $key => $value) {
    echo $value['ID'];

Another important issues is that you're using $results[$key] but you never assign it an initial value, and assume it's an array, you should initialise it using $results[$key] = arrayy(); first

  • This didn't seem to work
    – user33056
    Commented Jul 6, 2015 at 21:54
  • Can you be more specific, how did it not work, what did it do instead?
    – Tom J Nowell
    Commented Jul 7, 2015 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.