Skip Navigation

[Resolved] Sorting Views by distance to current post geolocation

This support ticket is created 5 years, 6 months 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by Christian Cox 5 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1128148

Hi,

I am trying to set up a view that lists the nearest other locations to the location set in the current post.

I have a Locations custom post type and all of the records have a geolocation field.

In the documentation I can see ways in the latest version of Toolset Maps (1.5) of sorting the view by the visitor's geolocation and by a fixed address.

But is this possible to sort by distance from an address set by the current post which is dynamic depending on the the geolocation field?

Many thanks for your help

#1128347
Screen Shot 2018-10-16 at 11.46.50 AM.png

Hi, if you choose "Distance center is set using shortcode attribute" in the Query Filter options, you will be able to pass any arbitrary value into the Query Filter using shortcode attributes. If you pass the current post's location into the shortcode attribute, you will have a dynamic distance filter based on the current post. Here's how that might look:

[wpv-view name="Your Map View" mapcenter="[wpv-post-field name='wpcf-fieldslug' id='$current_page']"]

Please be aware that you must click "Close" in the Distance Query Filter for any changes to take effect (see screenshot).

#1129216

Hi
Thanks for your reply.

I'm still not sure I can do what I need here.

Rather than list all of the locations that are within a distance radius from the current post, I need to list the (lets say) 5 nearest other location posts and order by distance from the current post.

Can this be done?

Thanks
Gary

#1129275

Okay I see what you mean. Ordering by distance from a variable location didn't make it into the most recent plugin release, but we are working on it for an upcoming release. Right now appears it will most likely be included in Toolset Maps 1.6. I don't have a timeline available for delivery just yet, but I see the developers are actively working on this improvement. I'll be glad to keep you posted here.

#1140488

Hi, Maps 1.6 has been released, and it includes some features that help you show distance between locations as well as sort Views by distance from an arbitrary location. Please download the latest maps version to get the newest features. One new feature includes a way to sort by distance from a location added as a URL parameter. However, in your case, it sounds like this isn't ideal, because you want to specify the distance from a location relevant to the current post. That would require a shortcode attribute, not a URL parameter. So a bit of custom code is required. Here's how you can do it:

Place the map View shortcode on the template for this post type. Add the "from_center" attribute to the map View shortcode, and set the value to be the custom address field value, like this:

[wpv-view name="Your Map View" from_center="[wpv-post-field name='wpcf-location']"]

My custom field has the slug "location" but yours may be different. Add the 'wpcf-' prefix to whatever the address field slug is in wp-admin.

Next, we added a new shortcode that allows you to display the distance from that location field in the current post. Copy + paste this shortcode into your View's loop if you would like to display the distance for each item:

[toolset-maps-distance-value location='[wpv-attribute name="from_center"]' postmeta='wpcf-location']

Again, you may need to change location here to match your field slug.

Finally, a bit of custom code is required to tell the View to respond to the from_center attribute we added to the View shortcode earlier. Add this to your child theme's functions.php file, or create a new code snippet in Toolset > Settings > Custom Code:

add_filter( 'wpv_view_settings', 'ts_sort_map_by_distance_from_shortcode', 99, 2 );
function ts_sort_map_by_distance_from_shortcode( $view_settings, $view_id ) {
  global $WP_Views;
  $views = array( 12345 );
  if ( in_array( $view_id, $views) ) {
    $shortcode_atts = isset( $WP_Views->view_shortcode_attributes[0] ) ? $WP_Views->view_shortcode_attributes[0] : null;
    if ( isset($shortcode_atts['from_center']) ) {
      $query_center = $shortcode_atts['from_center'];
      $view_settings['distance_order'] = array(
        "source" => "fixed",
        "center" => $query_center
      );
    }
  }
  return $view_settings;
}

Replace 12345 with the numeric ID of this View.

#1140489
Screen Shot 2018-11-05 at 10.31.33 AM.png

I should also include this image showing the required Ordering settings for this View. Note that I have chosen "as a distance from a URL parameter" even though this will be overridden by the custom code.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.