Skip Navigation

[Resolved] Split: View filter by distance – How to sort by distance

This support ticket is created 3 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Minesh 3 years ago.

Assisted by: Minesh.

Author
Posts
#2443981

How can I add a sort by distance to the view shortcode?

#2443997

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As a base from what distance you want to sort the results, is it from the parent location address as a base to sort it?

#2444001

Hi Minesh. That's correct.

So for this page the Auburn Listing should appear second as the distance is 1.1 from the current_page location.

hidden link

If possible I would also like to have 'Verified' items that will automatically sort to the top of the pile, and ignore distance.

#2444051

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

To set the distance order by, I've set the ordering section to order by the lat-long field ASC with Distance from "fixed" source.
=> hidden link
Screenshot: hidden link

Then to set the fixed distance as your current posts lat-long value, I've added the following filter to "Custom Code" section offered by Toolset:
=> hidden link

add_filter( 'wpv_view_settings', 'func_set_distance_orderby_address', 10, 2 );
 function func_set_distance_orderby_address( $view_settings, $view_id ) {
    if ( $view_id == 88 ) { 
      	global $post;
      
        $view_settings['distance_order']['center'] = get_post_meta($post->ID,'wpcf-post-lat-long',true);
     
    }
    return $view_settings;
} 

More info:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

Can you please confirm it works as expected now.