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
#1889813

Tell us what you are trying to do?
Display view of nearby places relative to the current post (has custom address field) using block editor. I have trouble setting the mapcenter with the block editor (there is no shortcode that older support articles refer to).

Is there any documentation that you are following?
https://toolset.com/course-lesson/filtering-and-ordering-map-markers-by-distance/

Is there a similar example that we can see?
https://toolset.com/forums/topic/i-was-trying-to-setup-distance-filter-on-a-view/

What is the link to your site?
hidden link

Every advice that I saw suggests adding the following:
[wpv-view name="list-of-apartments" mapcenter="[types field='map-apartments' output='raw'][/types]" cached="off"]

But the block editor no longer uses the shortcode.

So given this new style of code:

<!-- wp:toolset-views/view-editor {"reduxStoreId":"views-editor-1609264425937","viewId":621,"viewSlug":"nearby2","previewId":622,"insertExisting":"0","wizardDone":true,"wizardStep":3} -->
<div class="wp-block-toolset-views-view-editor wpv-gutenberg-view-wrapper-621"><!-- wp:toolset-views/view-layout-block -->
<!-- wp:toolset-views/view-template-block {"storeId":"views-editor-1609264425937"} -->
<div class="wp-block-toolset-views-view-template-block wpv-block-loop-item php-to-be-replaced-with-shortcode" data-toolset-views-view-template-block="1"></div>
<!-- /wp:toolset-views/view-template-block -->
<!-- /wp:toolset-views/view-layout-block --></div>
<!-- /wp:toolset-views/view-editor -->

How do I inject a mapcenter attribute?

#1890655

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Yes, currently with Block Views, you can not add the shortcode attribute to filter your view with the shortcode attribute.

For now, the only solution would be to use the classic views and use the method that is suggested:

[wpv-view name="list-of-apartments" mapcenter="[types field='map-apartments' output='raw'][/types]" cached="off"]
#1890979
Screenshot 2020-12-31 030719.jpg

Minesh, could you explain why this does not work (see screenshot)? If only this shortcode [types field='map-apartments' output='raw'][/types] resolved into a fixed address, it would work...

#1891259

Minesh
Supporter

Languages: English (English )

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

The option you highlight is used to only display the address string, it will not parse the shortcode.

I've another way but that will require add the view's API hook that could work. So you have two options, either go for classic view or send me problem URL where you added your view with access details so that I should try to add a view's API hook and see if that help you to resolve your issue.

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

#1891441

Minesh
Supporter

Languages: English (English )

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

Thank you for sharing access details but when I try to submit the admin access details to login form and when I click on submit button I can see the white page with the error: 502 Bad Gateway

Can you please send me working site so I can login to your system and try to make changes accordingly.

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

#1892925

Sorry about that Minesh. The login should be working ok now.

#1893669

Minesh
Supporter

Languages: English (English )

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

I've added the view's filter hook "wpv_filter_query" to the custom code section offered by Toolset as given under:
=> hidden link

function func_filter_distance_center( $query_args ,$view_settings, $view_id ) {
    global $post;
     
    if ( $view_id == 625 ) {
        
      $view_settings['map_distance_filter']['map_distance_center'] = get_post_meta($post->ID,'wpcf-place-address',true);
         
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'func_filter_distance_center', 10, 3);

Can you please confirm it works as expected now.

#1893847
Screenshot 2021-01-04 174033.jpg

I like the approach - thank you for this idea! For my general education - is there an easy way to view all the views and their IDs? In this case, how do I know that view 625 is in fact view named nearby3?

In any case, I *think* I could make the snippet less fragile by just using this conditional instead of if ( $view_id == 625 ):

if ( $post->post_type == 'place' )

And final question - given the snippet above, which option do I chose in the screenshot? I tried the top 3 but it didn't filter anything out... But maybe it was just a matter of me using the wrong view id 🤷‍♀️

#1893859

Minesh
Supporter

Languages: English (English )

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

You can able to see the View IDs here with the view's listing page:
=> hidden link

And final question - given the snippet above, which option do I chose in the screenshot? I tried the top 3 but it didn't filter anything out... But maybe it was just a matter of me using the wrong view id
==>
you should use the option "Distance Center is set using the fixed location" and add some dummy distance address value there as it will be overridden by the hook value.

#1894097

That was expertly resolved, Minesh! Works great! Thank you 🙏 Should be part of the UI and not require custom code though 😅

#1894259

>>you should use the option "Distance Center is set using the fixed location" and add some dummy distance address value there as it will be overridden by the hook value.

I spoke too soon... I've set a far away place into the fixed location, and no items are being returned... The mapcenter is being ignored...

#1894531

Minesh
Supporter

Languages: English (English )

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

There are two things:
1 Setting a map centre
2 Filtering the view results with address string coming from your custom field: [types field='place-address'][/types]

Currently, we implemented 2 and I set the shortcode "[types field='place-address'][/types]" to the option "Distance Center is set using the fixed location" as address value. Does it works now? If yes, this is the way it should work, as this is a workaround.

#1894615

As you mentioned yourself, "Distance Center is set using the fixed location" does not accept a shortcode. I tried putting "[types field='place-address'][/types]" into it, but it has no effect.

I also played a bit more with URL param option, and changing to add_filter() priority from 10 to 100, so that it runs later and overrides the empty URL param, but that didn't have any effect either...

#1894745

Minesh
Supporter

Languages: English (English )

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

As you mentioned yourself, "Distance Center is set using the fixed location" does not accept a shortcode. I tried putting "[types field='place-address'][/types]" into it, but it has no effect.
==>
yes, but is query filter is active. I asked you to put a shortcode a dummy value, as we are overriding the value of shortcode with actual address field value of your current post using the filter I shared here:
=> https://toolset.com/forums/topic/display-view-of-nearby-places-relative-to-the-current-view-using-block-editor/#post-1893669

As said, we have two things:
1 Setting a map centre
2 Filtering the view results with address string coming from your custom field: [types field='place-address'][/types]
==>
What option you want?
- Do you want to set the map center or filter the view results using the custom address field?

As I understand, your requirement is to filter the view result with the current post address filed "place-address" and display that marker on the map - correct? If yes, on what post its not working, Can you please share URL of that post?

If I misunderstood something here, please feel free to correct me. 🙂

#1895339

Ok, I see we are going in different directions 😅

>>What option you want?
Any option that works... I just cannot do [wpv-view name="nearby" mapcenter="[types field='place-address' output='raw'][/types]" cached="off"] because I plan on adding a lot of styling with the block editor.

The goal is to view a Place, and show a grid of Places nearby. I don't need to show any map at all. No visual map - just a grid view.

For testing - here are all the places - hidden link

hidden link - this place should have a lot of nearby places.

hidden link - this place is far away and should not have anything nearby.

I think we are really close, but not sure why it is not working...