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(
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) {
$loop->the_post();
$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.
$_GET
values to prevent injection attacks – Tom J Nowell♦ Jul 6 '15 at 20:54