Tell us what you are trying to do?
I'm trying to create a view that shows similar posts in the vicinity. So, I need to filter the posts based on distance, but I don't see an option to filter that value against the current posts address.
Is there any documentation that you are following?
https://toolset.com/course-lesson/filtering-and-ordering-map-markers-by-distance/
What is the link to your site?
Here's a page that shows a company. At the bottom ("Andere bedrijven in de nabije omgeving") there are three other companies that are near the current company.
hidden link
Hello, the distance filter can be configured to respond to a shortcode attribute, but shortcode attributes are not supported in the Block Editor at the moment. If a legacy Views approach is acceptable, you can adjust the View's Distance Query Filter to respond to a shortcode attribute like "mapcenter", and pass the current post's custom address field value into that shortcode attribute using another shortcode. However, this would mean that the View is not editable in your template using the Block Editor. The resulting shortcode would look something like this:
[wpv-view name="your-map-view" mapcenter="[types field='your-address-field' item='$current_page'][/types]"]
You would replace your-map-view with the slug of the map View, and replace your-address-field with the slug of your custom address field.
If you're using the Block Editor to design the Bedrijf post type Content Template, you could insert this nested shortcode combination using a custom HTML block. Otherwise, insert the nested shortcode in the template editor like any other shortcode.
Hi Christian,
Thanks for the reply, this helps!
One question, though: this filters the results, but doesn't sort right? In the legacy View I don't see an option to sort it as distance from a shortcode. Could you shed some light on this?
Ah yes, sorting by distance from an arbitrary location requires some additional custom code to modify the query a bit using a shortcode attribute instead of a URL parameter. See the example here for more details: https://toolset.com/forums/topic/sorting-views-by-distance-to-current-post-geolocation/#post-1140488
Let me know if you have questions about that.
Okay, so I've added the following shortcode in a Shortcode Block:
[wpv-view name="gerelateerde-bedrijven-legacy" from_center="[wpv-post-field name='wpcf-bedrijf-adres']" mapcenter="[types field='bedrijf-adres' item='$current_page'][/types]"]
And I added the code you mentioned in the link to my functions.php file of the child theme as follows :
add_filter( 'wpv_view_settings', 'hvdt_sort_map_by_distance_from_shortcode', 99, 2 );
function hvdt_sort_map_by_distance_from_shortcode( $view_settings, $view_id ) {
global $WP_Views;
$views = array( 19183 );
if ( in_array( $view_id, $views) ) {
$shortcode_atts = isset( $WP_Views->view_shortcode_attributes[0] ) ? $WP_Views->view_shortcode_attributes[0] : null;
if ( isset($shortcode_atts['from_center']) ) {
$query_center = $shortcode_atts['from_center'];
$view_settings['distance_order'] = array(
"source" => "fixed",
"center" => $query_center
);
}
}
return $view_settings;
}
Attached is a screenshot of the View settings.
But alas, I don't get any results. Any ideas?
First, I would verify that the wpv-post-field shortcode produces the expected results. You can try placing that shortcode just before the View shortcode, like this:
Test address field output: [wpv-post-field name='wpcf-bedrijf-adres']<br />
[wpv-view name="gerelateerde-bedrijven-legacy" from_center="[wpv-post-field name='wpcf-bedrijf-adres']" mapcenter="[types field='bedrijf-adres' item='$current_page'][/types]"]
Is the correct address output just before the View on the front-end of the site? If not, perhaps the $current_page item attribute is necessary, depending on where this field is placed in the template:
Test address field output: [wpv-post-field name='wpcf-bedrijf-adres' item='$current_page']<br />
If that doesn't solve the problem, I may need to log in and take a closer look.