Skip Navigation

[Resolved] Distance filter

This support ticket is created 3 years, 2 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.

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 44 replies, has 2 voices.

Last updated by ilonaB-2 3 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#2178325

Hi there,

About a week ago I needed help with the following 2 issues (see below):

As suggested by Jamal I need more help on how exactly to achieve a solution. Hope to hear back asap.
PS. The reply's i got didn't help me. Please provide me with the exact code or shortcode and instructions on how and where to implement it.

1. In the search results i'd like to have the post that is nearest to the users, filled in, location to be shown first and the same for the next results. Is this possible and if so, how do i do this?

2. Also i'd like to know if it's possible to show the distance in each search result.

Jamal replied with the following answers:

1. I am not really sure to understand this question, but I think that you want to order results by their distance from the user's location, right?
If that's the case, you need to order the view by distance from the user. However, this feature is not available yet for the blocks based views. You need to build the view with the legacy editor. You can activate it from Toolset->Settings->General(tab)->Editing experience. If you need help with it. Please open a new ticket and allow me temporary access to your website, so I can show you an example.

2. Yes, you can. You will need to use the toolset-maps-distance-value shortcode. If you want the distance from the user's location, this shortcode MUST be wrapped inside the wpv-geolocation shortcode.
Read more about these shortcodes here https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/

Thanks in advance.

Regards,

#2179391

Hello and thank you for contacting Toolset support.

Unfortunately, we have evolved our documentation about the Blocks editor and we have lost some articles about the legacy editor, where we need to build this view. For that reason, I prefer to show you an example. Would you allow me temporary access to your website to prepare an example for you? I'll need to know what post type you are working on and what custom field is used for the address?

Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#2181789

Hello, of course, we are here to help. It is rather easy, but I understand that you may not be familiar with the legacy Toolset editor. Here how it goes:
1. Create a new view from Toolset->Views.
2. Choose the custom post type.
3. Choose to sort by the address field, and compare the values as a distance.
4. Create the content of the view. Use the loop wizard as a starting point.
5. Add the distance value shortcode from the "Fields and Views" button.
I recorded this on a short video to show you how hidden link

More about Toolset Maps shortcodes here https://toolset.com/documentation/programmer-reference/maps/maps-shortcodes/#wpv-geolocation

#2181801

Hi Jamal,
Thank you so much for your support on the subject ánd for the video you made to explain the steps visualy.

I do have another question:
If I understand correct the distance will be determined by the current users location, right?

Is it possible to make it work that when a user fills in a location in the "enter a location" field the distance wil be determined from that location, but if the user doesn't fill in the "enter a location" field the distance is determined by the users actual location?

I hope you understand what i'm trying to discribe 🙂

#2181807

Well, maybe, maybe not.

If you are talking about the calculated distance, you can have a conditional shortcode on the entered_distance url parameter. That way, if the user provides it, you display a shortcode that will calculate the distance from there. If not, you display a shortcode that will calculate the distance from the user's location.

If you are talking about sorting the view depending on the entered_address(when the user enters an address, sort the view based on the distance from that address. When the user does not enter an address, sort the view based on the distance from the user's location), then it is not possible. You will need to have two views, and display one of the views based on the url_paramter of the address that the user entered.

You can use the wpv-search-term shortcode to get the value that the user may or may not have entered for the center. https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-search-term

Does it make sense?

#2181821

Hi Jamal,

It makes some sense. I just dont have a clue to get this done!

I'm almost afraid to ask but can you explain it to me as detailed as possible (with each step)? So i can do this myself?

Regards,

#2181943

First, I need to know what do you want to affect:
- The ordering(based on entered address or user's location)?
- Or the calculated distance displayed for each post?

#2181967
Zoekresultaten example.png

Ok,

So i'd like to, first order the search results based on distance from the entered location (the closest first) and show the actual distance in each search result (see attached image- red box as an example).

I hope this makes sense for you.

#2182153

This means that the user has entered an address to search against, right?

But, if he does not enter an address? For example, in the first load of the page, no address has been entered yet, what order do you want?
I assume that you want distances to be calculated from user's distance, right? Correct me if I am wrong.
But, what about the order?

#2182165

When someone visits the homepage, there won't be any search results untill they press the "Zoek" (search) button.

I think the best way is to start with their location as the centerpoint unless they actually fill in a location in de "enter a location" field.

If it's only possible to do one or the other then I'd want the distance to be calculated from the "enter a location" field.

Is this do-able?

#2184555

Yes, that's doable. However, having no results until the user enters a center location will require a custom code. Check this example https://toolset.com/forums/topic/hide-results-until-the-user-enter-a-value-in-the-filters/#post-1865593

I'll let you prepare it and if you need further assistance with it, let me know and I'll do my best to help.

#2187471

Hi Jamal,

I added the code, however I don’t see any changes or am I missing something?

Hope to hear back soon since we’ll be going live soon and it seems I’m not making any progress adding the distance in the search results (which is mandatory before going live).

Is there perhaps a way of getting in direct contact, to speed things up?

Regards,

#2187631

Well, the code is meant to:
- Be run only for the view with ID 24903, but no view exists that has this ID. Line 14.
- Check for a category of organizations to be chosen in the filters.
- Check for the distance to be defined.

I believe the code needs to be adapted, but I do not know what view should it target. Please, let me know the URL in the frontend where we can see the view and I'll check it further.

#2187639

Ok, so on the backend the view id is: 3323
I'm not sure how to:
- Check for a category of organizations to be chosen in the filters.
- Check for the distance to be defined.

The url on the frontend where the results should be shown is: mijnwingman.nl/zoekresultaten/

Thanks in advance for your reply!

#2187671

Thank you! I guess you only want to check against a location, not against the other filters(specialisatie, projekt, talen)!

If that's the case, this code should work:

add_filter( 'wpv_filter_query', 'empty_view_results_if_no_filters',99,3 );
function empty_view_results_if_no_filters( $query_args, $views_settings, $view_id) {
  global $WP_Views;
 
  // comma-separated list of view IDs where you want to apply this filter
  $view_ids = array( 3323 );
 
  // if in the right view
  if (in_array($view_id, $view_ids)){
      // Check if there is a distance filter, both the center and the radius
      $distance = ( isset( $_GET['toolset_maps_distance_radius'] ) && isset( $_GET['toolset_maps_distance_center'] ) ) ? ( isset( $_GET['toolset_maps_distance_radius'] ) && isset( $_GET['toolset_maps_distance_center'] ) ) : false;
       
     if ( !$distance ) {
          // empty the results
          $query_args['post__in'] = array(0);
      }
  }
  return $query_args;
}

If you want to include the other filters too, the code will need to be adapted.

Please check the results and let me know your feedback.