Skip Navigation

[Resolved] How to show map results, distanced by miles with exceptions

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 29 replies, has 3 voices.

Last updated by simonC-7 1 year, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2577831

**Tell us what you are trying to do?
I have a site that allows a visitor to search for companies within a distance radius. It works well.

I now need to be able to mark some of the companies as 'national' and give the visitor the search option: 'Include national companies'. Then, even if the search is only a 5 mile radius from the location, any company marked as 'national' will also appear in the results.

**Is there any documentation that you are following?
I can't find any docs that help me understand how I can achieve this.

**What is the link to your site?
Here is the site as it stands, without the option to include 'national' companies: hidden link

#2577869

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Simon

The map search by distance is based on point coordinates, so even if you created a post and gave it an address of "England", for example, it will still have a specific location somewhere in the midlands, and wouldn't show up in a search for Newcastle, for example.

A different approach would be to use a separate field (e.g. a checkbox field) to specify that a company is national.

Users could then filter for "national" companies, where the search would be based on the value of that custom field when checked, rather than its location.

But it would be either/or. I don't see how to combine the two in a single query. The distance query is done in such a way that it always applies when set, so that it would effectively operate with an AND comparison rather than an OR comparison when combined with other filters. (Results would have to be "national" AND within 5km.)

That also applies if you are modifying the query arguments before the query runs (using wpv_filter_query API hook).

You could use the wpv_filter_query_post_process hook that will include the results filtered by distance to run another query with get_posts where you retrieve the national results and then merge them. The problem there is that your pagination will be broken, if you use it.

Would there be many results? Is pagination needed?

See: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

Otherwise I think the only solution would include 2 Views. You would have the normal distance-filtered View, while a second View would output the national results separately (e.g. before the local results) wrapped inside a conditional shortcode (or block) so that it was only output when the national results were required.

Would something like that work?

#2577887

Thanks very much Nigel.

I have added a checkbox 'National Company' to filter on.

I am not using pagination, but using the wpv_filter_query_post_process hook and merging the results sounds beyond my capabilities.

I suppose the '2 views' approach could work. I could show the 2nd view under the local results with a heading 'National Companies'. Would the 'National companies' show on the map?

#2578259

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Where should the national companies show on the map?

Somewhere in the middle of the country? And if you have 10 national companies they all show at the same location?

Or are these local companies that offer national services, e.g. there might be one based in Glasgow and you want it to appear on the map (in Glasgow)? (That will mess with the scale of your map that shows local results within 5km, and all of those markers may then appear more-or-less on top of each other.)

If you do want to include the markers from the second View on the same map, you can do it by using a shortcode in the output of the second View that references the ID of the existing map. We can discuss that if you think it is a good idea.

#2578307

Thanks. The National companies offer national services from their headquarters, so it is the headquarters that would show on the map. I appreciate that will impact the scale of the map. It's good to know that the second view will show on the map if needed.

#2578375

The client has decided there's no need to show National Companies on the map, just listed underneath the local results.

So hopefully a second View of National Company results underneath the local results will work.

Now I just need to understand how to have 2 Views on the page:
- In the post type I have added a custom field 'National Brokers' and clicked it in two of the companies profiles.
- In the first View I have added the checkbox 'Include National Brokers' into View Search < this is causing a problem where I can't remove it from the filter

I am now struggling to understand how I configure the 2nd View to show the National Brokers.

Please can you assist? I can supply login details to the staging site so you can see my workings.

#2578381

Minesh
Supporter

Languages: English (English )

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

Sure - Can you please share problem URL and and admin access details and let me review your current setup and check what are the possible solutions.

Let me review if its possible to use two views or one view can even do. Please let me know what section you want to filter and what is your expected result when user click on checkbox filter.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2578419

Minesh
Supporter

Languages: English (English )

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

Ok I checked on your page:
- hidden link

I'm not sure why you added two views one with custom search and one that only shows result (without custom search) for the same post type Experts.

Or you want one view where you want to display the results with custom search that has location search that is why you added another view. So what you want is when user click on the checkbox "National Broker" irrespective of other filters it should include all the posts where post is set has National Broker?

#2578421

Thanks Minesh, almost as you said in your last paragraph:

What I want is when user click on the checkbox "National Broker" irrespective of the distance filter it should include all the posts where post is set to National Broker. ie. It should still filter for 'cover-for' and 'products'.

#2578469

Minesh
Supporter

Languages: English (English )

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

I see with the 2nd view you added - you are displaying the different content. If its a case that you will require to display the different fields (content) with the view's loop of your view 1 and view 2 then you will require two views.

so in that case how you want that view 2 should work. Should it display any result when page is loaded first time? or you want that when user select the "National Broker" from the view 1 and filter the result at that time only you want to display the only posts that is set as "National Broker".

#2578473

Thanks Minesh, I only added View 2 because I thought that was the way to get the solution as described by Nigel above.

I don't need 2 views necessarily. To repeat my requirement from the top of this post:

I now need to be able to mark some of the companies as 'national' and give the visitor the search option: 'Include national companies'. Then, even if the search is only a 5 mile radius from the location, any company marked as 'national' will also appear in the results.

#2578521

Minesh
Supporter

Languages: English (English )

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

I've added the following filter code to "Custom Code" section offered by Toolset with the code snippet "toolet-custom-code":
=> hidden link

add_filter('wpv_filter_query_post_process', 'func_include_national_posts', 10, 2);
function func_include_national_posts($query, $settings) {
    if ($settings['view_id'] == 1546) {
       
        $national_posts = get_posts(array(
            'posts_per_page' => -1,
            'post_type' => 'expert',
            'meta_key' => 'wpcf-national-broker',
            'meta_value' => 'National Broker'
        ));
       
       
      $merge = array_merge( $query->posts, $national_posts );
      $result = array();
      foreach($merge as $k=>$v):
      	$result[$v->ID] = $v;
      endforeach;
     
      $query->posts =  array_values($result);
      $query->found_posts = count($result);
         
    }
    return $query;
}

Can you please check and confirm it works as expected. you can remove your 2nd view as its not required.

#2578565

Thanks Minesh, your help is much appreciated.

It looks like the National Brokers are not being filtered by 'Products' (Health insurance, Cash plan, Travel etc) and 'Cover for' (Personal Policy, 3 to 99 employees, 100+ Employees). They need to be filtered by those.

It also looks like the National Brokers are showing even if the 'National Brokers' box is not checked in the search form.

Is it possible somehow to add a heading above the National results : 'National Brokers'?

Is it possible to put them in order of miles distance?

In the results list is there a way we can remove the [toolset-maps-distance-value] from showing for National companies?

#2579725

Minesh
Supporter

Languages: English (English )

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

Can you please check now.

I've adjusted the code added to the "Custom Code" section with the code snippet "toolset-custom-code" as given uunder:

add_filter('wpv_filter_query_post_process', 'func_include_national_posts', 10, 2);
 
function func_include_national_posts($query, $settings) {
    if ($settings['view_id'] == 1546) {
       
	 $qry_args = array();	
      if(isset($_GET['wpv-wpcf-national-broker']) and $_GET['wpv-wpcf-national-broker']=='National Broker'){
          $qry_args = $query->query['meta_query'];
      
    	$national_posts = get_posts(array(
            'posts_per_page' => -1,
            'post_type' => 'expert',
            'meta_query' => $qry_args ));
       
      }else{
        $national_posts = get_posts(array(
            'posts_per_page' => -1,
            'post_type' => 'expert',
            'meta_key' => 'wpcf-national-broker',
            'meta_value' => 'National Broker'
        ));
      
      }
      $merge = array_merge( $query->posts, $national_posts );
      $result = array();
      foreach($merge as $k=>$v):
      	$result[$v->ID] = $v;
      endforeach;
     
      $query->posts =  array_values($result);
      $query->found_posts = count($result);
         
    }
    return $query;
}

Can you please confirm it works as expected now.

#2579775

Thanks Minesh, it does not work as expected.

When I search:
Show results within 5 miles of High Wycombe HP12, UK
- Personal Policy - selected
- Health insurance - checked
- National Broker - NOT checked

The first two 'radius' results are correct.
The 3rd and 4th result are National brokers who should not show, because include 'National Brokers' was NOT checked.

When I search:
Show results within 5 miles of High Wycombe HP12, UK
- Personal Policy - selected
- Health insurance - checked
- National Broker - CHECKED

The first two 'radius' results are missing, they should show.
The 'National broker' result shows as expected.