Custom order by posts by custom taxonomy calculated values

Viewed 14

I will try to explain the issue as throughly as possible. I have a custom post type called Company. A company has a custom field Main City that takes a custom taxonomy (Location) as value. Location taxonomy has 2 fields: X and Y coordinate. I have Location taxonomy pages that show Companies that are have a specific location (not necessarily the main city, because a company can have multiple Locations).

My goal is to order Companies by the distance between a company's main city and the taxonomy of the page (so for example, I go to New York City taxonomy page and I want the companies to be ordered by the distance between their main city and New York City). I've been struggling to find any good solutions for that for over a week and still nothing.

Below is my function for calculating distance between two points on the globe (it works, i've used it to order location taxonomies):

function calculateDistance($lat1, $lng1, $lat2, $lng2) {
    $lat1 = deg2rad($lat1);
    $lng1 = deg2rad($lng1);
    $lat2 = deg2rad($lat2);
    $lng2 = deg2rad($lng2);

    $distance = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($lng1 - $lng2));

    return 6371 * $distance;
}

Here is my query for the posts:

<?php
        $term_slug = get_query_var( 'term' );
        $taxonomyName = get_query_var( 'taxonomy' );
        $city = get_term_by( 'slug', $term_slug, $taxonomyName );

        if ( get_query_var('paged') ) {
        $paged = get_query_var('paged');
        } elseif ( get_query_var('page') ) {
        $paged = get_query_var('page');
        } else {
           $paged = 1;
        }
        $args = array(
            'post_type' => 'company',
            'post_status' => 'publish',
            'posts_per_page' => 20,
            'paged' => $paged,
        );
         $args['tax_query'][] = array(
            'taxonomy' => 'location',
            'field' => 'slug',
            'terms' => $city->slug
        );
        $search_query = new WP_Query($args);
?>

My idea was to use $wpdb but I have no idea how to implement that. Any tips, help or code would be greatly aprreciated.

0 Answers
Related