Skip Navigation

[Resolved] Sort distance filter results from closest to most far away and output distance

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by Waqar 10 months ago.

Assisted by: Waqar.

Author
Posts
#2679129
sorting-details.jpg
Screenshot_distance-Filter.jpg

Tell us what you are trying to do?

Like I said I use divi and Views Legacy Editor.
To show results of my views distance filter I use the following shortcode and it works good (Filter Details in image).
[wpv-view name="filter-150" mapcenter ="[types field='adresse-city' output='raw]

I have 2 Problems:

1. Sort Filterresults
I want to sort the filterresults also as a distance of current post "city-adresse".
So the nearest post according to actual city-post should be displayed first. Then the second, and so on (see image).

2. Output Distance to Post as a Text
I tried a lot of codes to get this, one example is:
[toolset-maps-distance-value origin_source='url_param' url_param='mapcenter' postmeta='wpcf-adresse']

In the "toolsets - field and view-selection" I changed the standardvalue: "toolset_maps_distance_center" to "mapcenter" due to the fact I already have defined the value of mapcenter in my divi output shortcode.
I tried different codes but nothing worked. --> Fix location output is working but thats not what I need.
But even if I leave it as it is there is no output.

How to use the URL parameter right for sorting and to show the distance output?

Is there any documentation that you are following?
yes I tried a lot here an example:
https://toolset.com/forums/topic/toolset-maps-distance-value-shortcode-not-working/

#2679356

Hi,

Thank you for contacting us and I'd be happy to assist.

To troubleshoot and suggest the next steps, I'll need to see how this view and its shortcode are set up in the admin area.

Can you please share the temporary admin login details, along with the link to the page with this view?

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

regards,
Waqar

#2679609

Thank you for sharing the access details.

1. Ordering of results:

In the view's ordering setting it is not possible to select a shortcode attribute as a source. There are only 3 options "Fixed", "Visitor location" and "URL parameter".

To overcome this limitation, you'll need a custom function, that will force the view with ID "987512431" ), order the results based on the value coming from the shortcode attribute 'mapcenter':


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( 987512431 );
  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['mapcenter']) ) {
      $query_center = $shortcode_atts['mapcenter'];
      $view_settings['distance_order'] = array(
        "source" => "fixed",
        "center" => $query_center
      );
    }
  }
  return $view_settings;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

2. Distance value:

To show a distance value between the value passed in the view's shortcode attribute 'mapcenter' and the current post's 'adresse' field, you can use the 'toolset-maps-distance-value' shortcode like this:
( ref: https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/#toolset-maps-distance-value )


[toolset-maps-distance-value location="[wpv-attribute name="mapcenter"]" postmeta="wpcf-adresse"]

Note the use of 'wpv-attribute' to get the value from the shortcode attribute 'mapcenter'.
( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-attribute )

#2679706

Can you please hide this in my message and your reply: view's name (for sake of discretion)
The code for sorting is not working.
I think its better not to use the "mapcenter" shortcode instead using the custom field of the city page "wpcf-adresse".
I entered the code in the toolset code snippet, tried to change the code but I'm not able to get it work.
Do I need to add something to my shortcode of wpv-view which I added to the citypage to display the filterresults?

#2679803

Thanks for writing back and I've removed the view's name.

I see that in your code snippet, you're using the view's slug instead of the ID. The 'wpv_view_settings' filter expects the second argument to be the View's ID.
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_view_settings )

Can you please change that snippet to use the View ID instead and then check the view's ordering again?

Important note: I see that the 'Post Types Order' plugin is active on the website too. It is known to affect the order settings of the WordPress queries in general, including the ones used by the views. It would be interesting to test the code with this plugin deactivated.

Once the code is working, you may have to adjust that plugin's settings like 'Auto Sort' and 'Admin Sort'.

#2679877

Hi Waqar,
I don't know what you just made. It seems like you have magic hands.
I activated the code you send the second time (first time I also used the View's ID and it didn't worked) and now it's working.
Thank you very much. Have a great day.