Skip Navigation

[Resolved] Display view of nearby places relative to the current view using block editor

This support ticket is created 4 years 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
- 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 21 replies, has 2 voices.

Last updated by Alexander Rothschild 3 years, 11 months ago.

Assisted by: Minesh.

Author
Posts
#1895821

Minesh
Supporter

Languages: English (English )

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

Ok - so as you want to filter your view results with nearby places, within what range you want to filter it. Within 20km area or 100km area?

As you can see with the following screenshot:
https://toolset.com/wp-content/uploads/2020/12/1890979-Screenshot_2020_12_31_030719.jpg
==>
you should adjust the value for "Filter Radius" field as well as you can also adjust the value for "What to show", as you need to mention you want to search within the given radius or outside of radius.

Can you please adjust those value, that should return the results.

#1895907

I want to filter by 1km radius. This place - hidden link - should show several places within 1km radius.

#1895959

Minesh
Supporter

Languages: English (English )

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

Ok - as you marked this ticket resolved once, the system automatically removes the access details you share with private reply. Can you please share access details and I would be happy to dive into your site again and find out why its not working as expected.

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

#1896009

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've adjusted the code to "Custom Code" section offered by Toolset as given under and used the hook: wpv_view_settings

add_filter( 'wpv_view_settings', 'func_filter_distance_center_address', 10, 2 );
  function func_filter_distance_center_address( $view_settings, $view_id ) {
  global $post;
    if ( $view_id == 625 ) { 
      
$latlong=types_render_field("place-address",array('format'=>"FIELD_LATITUDE,FIELD_LONGITUDE",'item'=>$post->ID));
   
      $lat = 0;
      $lon = 0;
      if(!empty($latlong)) {
        
   			$latlon =  explode(",",$latlong);
        	
   			$lat = $latlon[0];
        	$lon = $latlon[1];
        
      		$view_settings['map_distance_filter']['map_center_lat'] = $lat;
        	$view_settings['map_distance_filter']['map_center_lng'] = $lon;
      }
        
    }
    return $view_settings;
}

Can you please confirm it works as expected now. 🙂

#1896019

This is it! Results shown in the view match what's nearby 👌 Nicely done Minesh 🙏

#2051769

[In need of additional advice]

This code has been working great, but there is one unpleasant limitation. If there are multiple views on a page, the map center gets applied to all of them, causing the other views to return no items found (since they are not map related).

With snippet enabled - hidden link (notice the missing quote, and the missing 'mentioned in' section)

With snipped disabled - hidden link (notice the broken nearby places - shows them all)

#2054217

Here's a final version of the code that limits the code to geo-based views only. The solution turned out to be properly detecting the geo-based views with the following line:

!empty($view_settings['map_distance_filter']) 

...and injecting the mapcenter only into the views that passed.

add_filter( 'wpv_view_settings', 'func_filter_distance_center_address', 10, 2 );
function func_filter_distance_center_address( $view_settings, $view_id ) {
     
  global $post;
    
  	//Only post_type 'place' has a place-address that can be injected - skip others
  	//Only geo-filtered views (have a non-empty map_distance_filter) need a map center - skip others
    if ( $post->post_type == 'place' && !empty($view_settings['map_distance_filter']) ) { 
      
		$latlong=types_render_field("place-address",array('format'=>"FIELD_LATITUDE,FIELD_LONGITUDE",'item'=>$post->ID));
   
        if(!empty($latlong)) {

              $latlon =  explode(",",$latlong);

              $view_settings['map_distance_filter']['map_center_lat'] = $latlon[0];
              $view_settings['map_distance_filter']['map_center_lng'] = $latlon[1];
        }
    }
    return $view_settings;
}