I have a custom post type location which has different categories (e.g. museums, theatres, shopping) and I want to output each of these categories and order within these categories by distance which is a custom meta field. Right now, I have following code, but cannot make the ordering work by distance.

        $custom_terms = get_terms('location_category');

    foreach($custom_terms as $custom_term) {
        $args = array('post_type' => 'location',
            'tax_query' => array(
                    'taxonomy' => 'location_category',
                    'field' => 'slug',
                    'terms' => $custom_term->slug,

         $loop = new WP_Query($args);
         if($loop->have_posts()) {;?>

At the moment, I'm using usort to order alphabetically (see below), but this needs to be changed to ordering by distance.

    <?php usort($loop->posts, function($a, $b) {
   return strcasecmp( 

Can anyone please help me with this?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.