Skip Navigation

[Resolved] Searching a Map View that Uses Shortcode Attribute…

This support ticket is created 2 years, 10 months 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 24 replies, has 3 voices.

Last updated by shawnW-3 2 years, 9 months ago.

Assisted by: Shane.

Author
Posts
#2102371

Seeing Paris on the initial load and knowing it can only be coming from shortcode attribute which is in the conditional wrapping is very promising. However, it's not working at all on mine site, and I see flaws on both mine and the sandbox.

On your site and mine, I notice mapcenter doesn't appear in the URL params when I 'use my location'. Perhaps that's insignificant, but it bears mention. On your site, I should be seeing New York as a result when searching out 2000 miles from my location - but I get the No Items Found text. In fact, outside of the initial on load showing Paris, the search won't show me any results anywhere after initiated. Please check to confirm.

Unfortunately on my site (also built out with mapcenter as the URL parameter) I'm not getting any filtering on first load - which is to say I'm getting all results on load using the 'url param view'. Because of this, the 'url param view' is not deferring to the 'when empty' section and so the conditional doesn't even come into play.

A few things I can confirm:
I built two views. URL Param View and Shortcode Attribute View.
URL Param View has the Rendered Map and Search Controls, while Shortcode Attribute View does not.
I have tried both mapcenter and toolset_maps_distance_center in the URL Param view. The latter does show in the URL.
I have tired if="( '[wpv-search-term param='mapcenter']' eq '' )" AND if="( '[wpv-search-term param="mapcenter"]' eq '' )" cause I know quote wrapping can be problematic on occasion.
I can confirm my shortcode is working, and I even tried it with a static value - which didn't make a difference.

Most of these don't matter though, because the default query for URL parameter IS loading in (all) results, and thus it won't defer to the conditional.

#2102405

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+00:00)

Sorry, I forgot to mention the link to the custom code that will empty the 2nd view if no URL parameter for map center is available. You can check it in the custom code snippet here hidden link

add_filter( 'wpv_filter_query', 'empty_view_when_no_url_param', 99, 3 );
function empty_view_when_no_url_param( $query_args, $view_settings, $view_id ) {
  if ( $view_id == 34 ){
    error_log('Do we have a mapcenter URL param: ' . ( isset($_GET['mapcenter']) ? ('Yes ' . $_GET['mapcenter']) : 'No') );
    $query_args['post__in'] = array(0);
  }
  return $query_args;
}

Would you allow me temporary access to your website to check it further? Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
Please provide URLs for the search page, the view, and let me know where did you add the custom code.

If you are not using Toolset->Settings->Custom Code section, I will also need FTP access. I wouldn't touch code in theme's files or a plugin without having FTP access.

#2102421

AH HA!

Yes. Now we're cooking. I'm going to have to do some tweaking and testing, but it's definitely looking good. I'm going to keep the ticket open until I've got it all worked out. Should be by the end of the day. Very clever solution. Please stand by.

Thank you

#2102599

This is really coming together nicely, but I've run into a smaller issue...

One of my nested views uses the query "Show posts outside of 200mi radius of address/coordinates provided using mapcenter shortcode attribute." This is for when there are no results inside the radius but we know there are results outside the radius.

Obviously, "outside" a radius includes everywhere on the planet. So for the exact same view, using URL Param, I would just limit the results to 50, and change the order to "Dealer Address - Ascending - As a distance from - URL Parameter - toolset_maps_distance_center" ... that would show me the 50 closest to the location searched.

However, with a shortcode attribute, this doesn't work as there is no URL parameter. If I try "user's location" it doesn't work and to do "fixed location" I would need a variable, but it won't let me enter one on the backend. I'm hoping there is a way to set the ordering with a shortcode that doesn't involve $view_settings.

Any ideas on that?

#2103295

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Shawn,

As Jamal is unavailable for the next 2 days I will be continuing the thread here.

It would appear that this issue is mostly solved however you're now having issues with the Distance filter.

Your comment below

One of my nested views uses the query "Show posts outside of 200mi radius of address/coordinates provided using mapcenter shortcode attribute." This is for when there are no results inside the radius but we know there are results outside the radius.

I understand here that you've made a fallback view for when there are no results for the inside radius, so you default to showing posts 200m outside of the map center radius.


Obviously, "outside" a radius includes everywhere on the planet. So for the exact same view, using URL Param, I would just limit the results to 50, and change the order to "Dealer Address - Ascending - As a distance from - URL Parameter - toolset_maps_distance_center" ... that would show me the 50 closest to the location searched.

From here I understand on the outside radius you want to show the 50 closest locations which would involve you limiting the view to 50 posts and sorting the view by distance.

I'm hoping there is a way to set the ordering with a shortcode that doesn't involve $view_settings.

For clarity, are you referring to the order of the posts by distance ?

Please let me know.
Thanks,
Shane

#2103315

I think I have this all solved.

I used the following php. If you see anything wrong with it, please let me know, but it's working without issue.


function bl_users_distance_from_shortcode( $view_settings, $view_id ) {

  if ( $view_id == 155777 ) {
      $userscenter = wpv_do_shortcode('[users_zip]');
      $view_settings['distance_order']['source'] = 'fixed';
      $view_settings['distance_order']['center'] = $userscenter;
  }
  return $view_settings;
}
add_filter( 'wpv_view_settings', 'bl_users_distance_from_shortcode', 5, 2 );

Basically this sets the ordering by distance to work with the same shortcode variable I use for mapcenter.

I really can't believe that's not just an option in the views. I found posts going back years about the lack of this feature. But in any case, this does seem to work fine in solving that issue.

#2103917

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Shawn,

I checked on your code and our views setting API and the code is correct.

I'm not seeing any issues.

Thanks,
Shane

#2103949

I did things a little differently and ended up with 5 views total.

A master view which contained the solution provided by Jamal as well as the map render, filter controls and output styling. The others were just results in the loop templates. A shortcode attribute filtered view for inside the search radius. A nested fallback in that view which handles shortcode attribute filtering outside the search radius. A URL Parameter filtered view for inside the search radius. And a nested fallback in that view which handles URL Parameter filtering outside the search radius.

This required the PHP to kill the first query and to change sorting behavior for views shortcode attribute views.

I know that Views are considered legacy because you're focusing on blocks and thus feature requests probably aren't considered... BUT, being able to set order by distance with a mapcenter shortcode attribute view should be a feature. Having a way for users to switch a view from shortcode to URL parameter following a search submit should be a feature.

While this is a highly complex workaround, I'm still very satisfied. Many thanks.

#2104271

Would any of this cause new information saved to not show up?

None of the custom PHP dealt with this view specifically, but I went ahead and disabled all the custom php, and no matter what I can't updated html content in this view to show up on the front end. No JS errors, though this is clearly not a JS type of malfunction.

The view is the Shortcode Attribute View Inside Radius - and the new content is definitely saved tested by refreshing the view on the backend and the content I just added is indeed still there. But on the front end there is no change. I've rendered each view separately on different pages and it does appear that this view is the only one of the five new views to have this issue. Also, to be clear the content that was in the view previous to the update is still rendering exactly as it would if I hadn't made the changes.

All caching (that I'm aware of) is disabled, and I haven't had any issues with caching on the other views.

I'm really perplexed by this.

#2104291

Agh, nevermind. The content template wasn't linked up. Good to reclose this ticket.

Apologies

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