Home › Toolset Professional Support › [Resolved] Views Search – show no results until search option entered, and add pagination
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)
Tagged: Custom search, Views, Views plugin
Related documentation:
This topic contains 30 replies, has 3 voices.
Last updated by zacharyL 5 years, 8 months ago.
Assisted by: Nigel.
Tell us what you are trying to do? Use AJAX custom search results for a Toolset View, and not show any results until a filter/search option has been selected/entered. When results are shown, show results with pagination
Is there any documentation that you are following? Just looking at example directory sites
Is there a similar example that we can see? /therapist-search/
What is the link to your site? Provided separately.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Mark
This comes up often enough, and I have a sample custom function you can use:
/** * No initial results * * Don't show View results until a filter has been applied * * Tests for custom field filters, taxonomy filters, or text searches */ function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){ $target_views = array( 226 ); // Edit to add IDs of Views to add this to if ( in_array( $view_id, $target_views ) ) { // if there is a search term set if ( !isset( $query_results->query['meta_query'] ) && !isset( $query_results->query['tax_query'] ) && !isset( $query_results->query['s'] ) ) { $query_results->posts = array(); $query_results->post_count = 0; $query_results->found_posts = 0; } } return $query_results; } add_filter( 'wpv_filter_query_post_process', 'tssupp_no_initial_results', 10, 3 );
You just need to edit the ID(s) of the View(s) you want to apply it to. You can add this code snippet at Toolset > Settings > Custom code.
Set up the View as normal, including the pagination settings you require when the results are displayed.
Thanks Nigel - will give this a try tonight.
Hmm, oddly when I give this a try, two things occur:
1) If I enter in a location, that doesn't appear to be enough to trigger the query to run, as it still says no results found.
2) When I do select another filter option, the results do come in, however, the formatting/layout is wonky for some reason.
Any issues you know of in doing this with AJAX search on? I haven't started implementing the pagination just yet either.
Thanks Nigel,
-Mark
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Actually, that's a generic function which doesn't allow for the special case of distance searches, which will need special handling.
Hold off from using it for the moment, and I'll take another look.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
OK, I updated the code so that it also listens for a distance search before returning no results:
<?php /** * No initial results * * Don't show View results until a filter has been applied * * Tests for custom field filters, taxonomy filters, text searches, and distance search */ function tssupp_no_initial_results($query_results, $view_settings, $view_id) { $target_views = array(72); // Edit to add IDs of Views to add this to if (in_array($view_id, $target_views)) { // if there is a search term set if (!isset($query_results->query['meta_query']) && !isset($query_results->query['tax_query']) && !isset($query_results->query['s']) && !isset($_REQUEST['toolset_maps_distance_radius'])) { $query_results->posts = array(); $query_results->post_count = 0; $query_results->found_posts = 0; } } return $query_results; } add_filter('wpv_filter_query_post_process', 'tssupp_no_initial_results', 10, 3);
This simply modifies the array of posts returned by the View and shouldn't in any way affect how the results are output.
Hey Nigel,
Added the updated version to my functions file - I couldn't find the area you were talking about otherwise (Toolset ->Settings -> Custom Code). It doesn't appear to work for location however. Can I provide you hidden access to see? You should have access to the public site through some of the other tickets I have open, so please feel free to take a look yourself (/t*******t-search/ is the page).
As well, I *think* I know why the layout of the results is messed up. I am using Beaver Builder to design the layout of each result, and I think because there are no results initially, the CSS for the design from Beaver Builder is not loading. Any idea to get this to load?
Cheers,
Mark
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Mark
OK, I checked your site and it is updating via ajax, mine was via page refresh, and the search parameters are submitted a little differently in that case, so I updated the code snippet one more time:
/** * No initial results * * Don't show View results until a filter has been applied * * Tests for custom field filters, taxonomy filters, or text searches */ function tssupp_no_initial_results($query_results, $view_settings, $view_id) { $target_views = array(37); // Edit to add IDs of Views to add this to if (in_array($view_id, $target_views)) { // if there is a search term set if (!isset($query_results->query['meta_query']) && !isset($query_results->query['tax_query']) && !isset($query_results->query['s']) && !isset($_REQUEST['toolset_maps_distance_radius']) && !isset($_REQUEST['search'])) { $query_results->posts = array(); $query_results->post_count = 0; $query_results->found_posts = 0; } } return $query_results; } add_filter('wpv_filter_query_post_process', 'tssupp_no_initial_results', 10, 3);
Regarding the CSS, you'll need to add it another way, not using the template designed with Beaver Builder. You could add it to the custom CSS section of the View itself instead.
If you notice any jank problems on page load, you could add it directly within one of the View edit sections inside style tags (remember that the View editors are just HTML sections, where the shortcodes will get replaced by HTML, and you can include any valid HTML there).
Thanks Nigel, this seems to work with the Ajax request. I am going to work on the CSS portion of this, and follow up on how I turn out.
Thanks,
Mark
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
OK, let me mark this as awaiting feedback from you.
Hey Nigel,
I realized this is not a case of missing CSS at all, but instead the results being sanitized or something of the sort. I disabled the function you generated for me, so that with no search filters provided, results are displayed properly with their Beaver Builder layout. This works on first page load, however, if I change any filter, or sort option, the results shown immediately are missing all div's, classes, id's, i's, etc, and is just a blob of <img> and <p> tags. If I click Reset then, the same results I saw initially are displayed, but once again, missing all of the ids, classes, divs, i's, etc... Is some sanitization process interfering here? You should still have admin access, so please login to see the page (most of the posts are private currently, but will show if you are logged in). Any thoughts about what is going on and how to fix it would be appreciated.
Cheers,
Mark
I realized that for some reason, when you choose a filter/search option, the view isn't looking at the Content Template / Beaver Builder template anymore. Instead, it is reverting back to what is actually entered in the text box (if you click the link, 'stop using Beaver Builder for this content template'). The markup there matched what was actually being displayed on the front end when I chose a search or filter option. This seems like a bug, unless I am missing something here.
-Mark
Confirmed this is the case. Here is how I reproduced this Nigel.
Went into the view and went down to 'Templates for the View'. Under 'Loop Item for ......... view' it said the template was created using Beaver Builder. I clicked 'Stop using Beaver Builder for the content template'. It then displayed the pre-existing markup in the box there, I simply wiped this markup fully, and clicked 'Update'.
I then clicked the 'Beaver Builder' button in the top right corner again, which then toggled Beaver Builder on for the template again. Now when I select a filter/sort option, no results display, but the map still updates. Obviously if I fill the loop markup box with something else instead of just erasing it completely as part of the process, that will show up in-place of the missing results. Please feel free to try this on the site directly yourself.
I am not sure if this is specific to AJAX results or not, but wasn't able to move away from AJAX results without the search stopping working. I am not opposed to this as a short term solution, but will need some help migrating over from AJAX to non-AJAX. When I tried (you can toggle it yourself) the search did not update at all.
Thank you Nigel,
Mark
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Mark
Having installed this locally and been able to play around with it I can see that, yes, the issue only happens when updating the search results via ajax, and it is to be expected.
I looked through the documentation for Beaver Builder and unfortunately we don't seem to mention it, though I know we do with Visual Composer, and that is when paginating or updating searches via ajax, non-Toolset shortcodes in the results are not parsed, so page builders such as BB or VC that operate with shortcodes won't work properly when the results are updated.
I changed the settings for your View on my test site to update the search via a page reload when a submit button (which I had to add) is pressed, and then the styling is retained after the results update.
Hey Nigel,
Was it simply a matter of changing the Ajax option to on-submit, and adding a submit button?
I would think if it is not supported at all that Beaver Builder shouldn't be an option with Ajax enabled then. Perhaps this could be something available in the future?
Thanks,
Mark