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 ago.

Assisted by: Minesh.

Author
Posts
#2579841

Minesh
Supporter

Languages: English (English )

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

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.
=====>
This issue was one of the tricky one.

With the above case as there is no way to find the missing 'radius' results. I've created the following view:
- hidden link

The above view will return those missing 'radius' results and I've used the above using using Toolset view's PHP API function: get_view_query_results()
- https://toolset.com/documentation/programmer-reference/views-api/#get_view_query_results

I've adjusted the code as given under:

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) {
      
       if(isset($_GET['wpv-wpcf-national-broker']) and $_GET['wpv-wpcf-national-broker']=='National Broker'){
        
       $without_found_ids = $national_posts = $without_national_posts = array();
       $qry_args = $query->query['meta_query'];
      
        $without_national_posts = get_view_query_results(4144);
        
        $national_posts = get_posts(array(
            'posts_per_page' => -1,
            'post_type' => 'expert',
            'meta_query' => $qry_args ));
        
           
      $merge = array_merge( $query->posts, $without_national_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;
}

I checked the both the cases you shared and I can see the expected results. Can you please confirm it works as expected now at your end as well.

#2579865

Thank you Minesh,

I have added another National company and they show up in the search too, which is great. But can they be listed in order of distance?

For my information:

Can you tell me how I can NOT show the 'National Brokers' on the map should I choose not to?

Is there a way I can put the heading 'National Brokers' above the 'national brokers' results?

How can I remove the [toolset-maps-distance-value] from the National Brokers results?

Lastly:

Eventually I will need to implement this on the production site, can you assist or list out for me the changes you have made on this staging site?

Many, many thanks for your help Minesh.

#2579897

Minesh
Supporter

Languages: English (English )

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

To sort the results by distance in (DESC) order I've again adjusted the code as given under:

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) {
      
       if(isset($_GET['wpv-wpcf-national-broker']) and $_GET['wpv-wpcf-national-broker']=='National Broker'){
        
       $without_found_ids = $national_posts = $without_national_posts = array();
       $qry_args = $query->query['meta_query'];
      
        $without_national_posts = get_view_query_results(4144);
        
        $national_posts = get_posts(array(
            'posts_per_page' => -1,
            'post_type' => 'expert',
            'meta_query' => $qry_args ));
        
           
      $merge = array_merge( $query->posts, $without_national_posts,$national_posts);
      $result =  $distance = array();
      foreach($merge as $k=>$v):
        
         $distance[$v->ID] = do_shortcode("[toolset-maps-distance-value origin_source='url_param' postmeta='wpcf-address' unit='mi' postmeta_id='".$v->ID."']");
         
        $result[$v->ID] = $v;
      endforeach;
          
        /// sorting by deistance
        arsort($distance);
         
      
         foreach($distance as $k=>$v):
         	$distance[$k] = $result[$k];
         endforeach;
         $result = $distance;
      
      $query->posts =  array_values($result);
      $query->found_posts = count($result);
        
       }
      
    }
    return $query;
}

For my information:

Can you tell me how I can NOT show the 'National Brokers' on the map should I choose not to?
==>
Yes, thats correct. You should not select'National Brokers'.

Is there a way I can put the heading 'National Brokers' above the 'national brokers' results?
==>
Yes, done. I've added the conditional block to your following view:
- hidden link

How can I remove the [toolset-maps-distance-value] from the National Brokers results?
==>
I've added the following conditional statement to hide the distance value for the "National Brokers":

[wpv-conditional if="( '[types field='national-broker' output='raw'][/types]' eq '' )"]
[toolset-maps-distance-value origin_source='url_param' postmeta='wpcf-address' unit='mi'] miles
[/wpv-conditional]

Lastly:

Eventually I will need to implement this on the production site, can you assist or list out for me the changes you have made on this staging site?
==>
1. You will have to apply the code I shared with the same view ID you are displaying that is added to the "Custom Code" section.
2. You will have to create the following view as well and make sure with the code snippet you adjust the view ID with the get_view_query_results() function:
- hidden link
3. you will have to add the conditoinal statements that I added to your following view:
- hidden link
3.1 To hide distance value for "National Brokers":

[wpv-conditional if="( '[types field='national-broker' output='raw'][/types]' eq '' )"]
[toolset-maps-distance-value origin_source='url_param' postmeta='wpcf-address' unit='mi'] miles
[/wpv-conditional]

3.2 And conditional block to display heading 'National Brokers' for national broker posts.

Glad to know that the solution I shared help you to resolve your issue.

As per our support policy - we entertain only one question per ticket. This will help other users searching on the forum as well as help us to write correct problem resolution summery. May I kindly ask you to open a new ticket with every new question you may have.

Thank you for understanding.

#2579915

Thank you very much Minesh. I am very grateful.

As it's specific to this case can I just ask a question that was mis-understood:

Can you tell me how I can NOT show the 'National Brokers' on the map should I choose not to?
==>
Yes, thats correct. You should not select'National Brokers'.
==>
What I meant is...
I would like the National Brokers to show in the results list, but not to show on the map. How could I do this?

#2579927

Minesh
Supporter

Languages: English (English )

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

Its the same like we hide the distance value.

I've wrapped the marker shortcode with the coordinal statement as given under:

[wpv-conditional if="( '[types field='national-broker' output='raw'][/types]' eq '' )"]
[wpv-map-marker map_id='map-5' marker_id='marker-[wpv-post-id]' marker_title='[wpv-post-title]' marker_field='wpcf-address']
<div class="map-marker-popup">
	<p>[types field='company-logo' title='%%TITLE%%' alt='%%ALT%%' align='center' size='medium' resize='proportional' class='map-marker-logo'][/types]</p>
	<h5 class="popup-heading">[wpv-post-title]</h5>
	<p><a class="popup-link-button" href="[wpv-post-url]">View details</a></p>
</div>
[/wpv-map-marker]
[/wpv-conditional]
#2579931

Sorry Minesh, nothing shows on the map at all now.

And please can the results be listed nearest first?

#2579933

Minesh
Supporter

Languages: English (English )

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

I've also deleted the marker from the "Map" block and changed the map_id to map-4 with your marker shortcode.

For example: map_id='map-4'

[wpv-conditional if="( '[types field='national-broker' output='raw'][/types]' eq '' )"]
[wpv-map-marker map_id='map-4' marker_id='marker-[wpv-post-id]' marker_title='[wpv-post-title]' marker_field='wpcf-address']
<div class="map-marker-popup">
    <p>[types field='company-logo' title='%%TITLE%%' alt='%%ALT%%' align='center' size='medium' resize='proportional' class='map-marker-logo'][/types]</p>
    <h5 class="popup-heading">[wpv-post-title]</h5>
    <p><a class="popup-link-button" href="[wpv-post-url]">View details</a></p>
</div>
[/wpv-map-marker]
[/wpv-conditional]
#2579937

Minesh
Supporter

Languages: English (English )

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

Please delete the cache and check.

And please can the results be listed nearest first?
==>
To change the sorting order I've changed the function from arsort() to asort() within custom code:

 /// sorting by deistance
        asort($distance);

I hope this is resolved now.

#2579939

My issue is resolved now. Thank you!

Minesh you have gone above and beyond, I am very grateful. Thank you.

#2580023

Hi, I am sorry to re-open this ticket but I am finding 2 problems:

1 - When I search with the example search we have been using it works as expected:
Show results within 5 miles of High Wycombe HP12, UK
- Personal Policy - selected
- Health insurance - checked
- National Broker - CHECKED and NOT CHECKED

All works as expected

But, when I change the search to 500 miles and - National Broker - CHECKED
It still only shows the 4 results (it should show many more)

When - 500 miles and - National Broker - NOT CHECKED it shows all the results as expected.

2 - When there are no results, eg:
Show results within 5 miles of Hull UK
- Personal Policy - selected
- Health insurance - checked
- National Broker - NOT CHECKED

The map is no longer defaulting to an empty UK.

#2581331

Minesh
Supporter

Languages: English (English )

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

As the ticket was marked resolved the system automatically delete the private information you shared like access details.

Can you please share admin access details and let me review that whats going wrong there.

*** 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.

#2583031

Minesh
Supporter

Languages: English (English )

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

Can you please check now.

The issue was the view that I created to fetch the distance search result, the query filter is set to query 5 miles.
- hidden link

As we are using the above view in PHP mode within the hook "wpv_filter_query_post_process":

$without_national_posts = get_view_query_results(4144);

The issue was when you change the miles in custom search it was still searching for only 5 miles that is added as Query filter. So to assign the dynamic miles when changed, I've added the following hook to "custom code" section:
=> hidden link

function func_update_distance_radius_from_urlparam( $view_settings, $view_id ) {
 
  if ( $view_id == 4144 ) {
    if(isset($_REQUEST['toolset_maps_distance_radius']) and 
       $_REQUEST['toolset_maps_distance_radius']!=''){
       $view_settings['map_distance_filter']['map_distance']= $_REQUEST['toolset_maps_distance_radius'];
    }
   
  
  }
  return $view_settings;
}
add_filter( 'wpv_view_settings', 'func_update_distance_radius_from_urlparam', 5, 2 );

The above hook will assign the miles value from the URL param.

Can you please check now I ca see its working as expected now.

You can mark resolve this ticket with different message as this ticket was already marked resolved before.

#2583033

Minesh
Supporter

Languages: English (English )

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

2 - When there are no results, eg:
Show results within 5 miles of Hull UK
- Personal Policy - selected
- Health insurance - checked
- National Broker - NOT CHECKED

The map is no longer defaulting to an empty UK.
==>
I'll split the ticket for this issue.

#2583907

Minesh
Supporter

Languages: English (English )

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

Can you please confirm the solution I shared help you to resolve your issue.

#2584047

Hi, thank you. Yes, that appears to show the results expected. Many thanks.

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