Skip Navigation

[Resolved] How to initially show no map until search performed

This thread is resolved. Here is a description of the problem and solution.

Problem:
A View displays results on a map. How to not show the map until the location filter has been entered by the user?

Solution:
You can split the View form and View results onto separate pages, so that on the first page only the form shows, and the map will only appear when the second page with the results is loaded.

When you insert a View with the Fields and Views button the UI will show the option to insert only the search form so that the results can be inserted on a different page.

This support ticket is created 6 years, 11 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 2 voices.

Last updated by Nigel 6 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#608822

For performance reasons I don't want the map to display any results until a user has entered their location. How exactly would I do it?

#608835

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Philippe

You need to add some custom code to your site, either to your theme's functions.php file or using a plugin such as Code Snippets.

Here is an example of the code required:

add_filter( 'wpv_filter_query', 'tssupp_no_initial_results', 101, 3 );
function tssupp_no_initial_results( $query_args, $view_settings, $view_id ){

  $affected_views = array( 54 ); // Add IDs of relevant Views

  if ( in_array( $view_id, $affected_views ) && !isset( $_REQUEST['toolset_maps_distance_radius'] ) ) {
	$query_args['post__in'] = array( 0 );
  }
  
  return $query_args;
}

What it does is check whether a distance filter is present. If not this must be the initial page load and so it sets a query argument that cannot be met, in this case that the post id is zero, so that no results are initially returned by the View. (Your View should not have the "Don't include current page in query result" option checked.)

When a distance filter is applied, the View results will be returned as normal.

I'm including the Loop Output section of my sample site below:

[wpv-layout-start]
[wpv-map-render map_id="map-1"][/wpv-map-render]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
          <h3>[wpv-post-link]</h3>
          [wpv-map-marker map_id="map-1" marker_id="marker-[wpv-post-id]" marker_field="wpcf-location"][/wpv-map-marker]
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		[wpv-map-marker map_id="map-1" marker_id="marker-[wpv-post-id]" address="London Bridge Street, London, United Kingdom"][/wpv-map-marker]
	[/wpv-no-items-found]
[wpv-layout-end]

Note that the wpv-map-render shortcode will draw the map whether there are any results or not.

When there are results to display the map markers from the matching posts are shown.

In the wpv-no-items-found section I added a map marker with a fixed location, so that the map doesn't initially show the middle of the Pacific Ocean. You may want to change this so that the map itself only displays when there are results found, for example.

#609059

Thank you, it works!

Right now, however, I'm using a custom map marker in the center of the United States with a "no results found" mention. I would like to avoid the users to land on a "no results found page" when they haven't entered any location yet.

Is it possible to do something like this?

- At first load of the page, the map doesn't show
- When a location is entered and the search is submitted, the map appears. If there're results, it shows the results markers. If they're no result in the current location distance then my custom marker is shown instead.

Right now, I can't show different things whether the search has been activated once or not since in both scenarios they're using what's inside the [wpv-no-items-found] of my view

#609410

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Philipe

In terms of where you insert the map, you can do so immediately after the wpv-layout-start shortcode, meaning it will be displayed regardless of whether there are any results or not, inside the wpv-items-found shortcode, so that it only shows if there are some results, or inside the wpv-no-items-found section, where it displays when there are no results.

The code I used above is to trick the View into thinking there are no results when the page first loads, which means the map added to the wpv-no-items-found section will be displayed.

An alternative is to dispense with the custom code I provided and show the View search form on one page and the results on another, which I think is the only way you can achieve what you describe.

So using the Fields and Views button insert the View onto your first page and specify that the View should show the filter section only with the results on a separate page. Then on a second page insert the View again, this time specifying it is the results to be displayed.

#609902

Thank you. Splitting the form and the results on different pages works well.

One last thing, when I enter the submit button it still take a bit of time (maybe 2 seconds). I guess it's because there's a lot of posts in my DB.

I can live with the delay (I'll add a warning for the visitors) but I was wondering if there's a way to reduce the delay? I guess a better hosting with more resources allocated to php will reduce it? Do you have any other way to speed the search?

Thank you.

#610114

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Philippe

There's not really anything you can do about that.

I've raised the issue with our developers about improving performance on large sites, and have actually passed a copy of your site for them to look at.

In the meantime the beefier your server the better performance you can expect, but there's nothing you can change in Toolset to optimise this any further.