Home › Toolset Professional Support › [Resolved] How to show no initial results with a custom search View?
Problem:
How to show no initial results with a custom search View?
Solution:
All posts are shown initially, and filters narrow-down the results, eliminating the non-matching ones.
You can add code that uses the Views API to detect whether any filters have been set, and if not (such as when initially loading the page) remove all of the results.
An example of such code is below: https://toolset.com/forums/topic/i-want-to-show-the-message-please-select-the-area-before-showing-the-selection/#post-1272533
Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process
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 plugin
Related documentation:
This topic contains 5 replies, has 2 voices.
Last updated by kongkritY 5 years, 6 months ago.
Assisted by: Nigel.
hidden link
Please help me
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Let's see if I understand what you want.
A custom search View includes a filter, for what looks like a taxonomy in your case.
Such filters work by narrowing down the results.
So the starting point is always "All results", and then when the user selects a filter the results update to show only the matching posts.
It doesn't look like you are showing any results, whether a filter has been selected or not.
I'm guessing that what you want is, before a filter is selected, to show NO results, and only after the filter has been selected show the matching results.
Would that be right?
And while showing no initial results you want to display a message repeating the call to first select the area.
yes
i want before is filtered, to show the results, and the selected results.
How do I set up?
That's right.
I want it before select a filter to show the message and after the filter has been selected show the matching results.
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
You can use the wpv_filter_query_post_process API hook (https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process) to modify the query so that, if no filter has been applied, no results are returned.
I've shared a generic solution using this before, let me repeat it here:
/** * 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( 123 ); // 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 need to add a custom code snippet with the above at Toolset > Settings > Custom Code, and you will need to edit the ID of your View ("123" in the example above, and you can provide a list of IDs here if it should apply to multiple Views).
So when your page first loads and has no filters set, you will get the "No results found" message which appears inside the wpv-no-items-found shortcodes in the View output. You can replace this with markup that displays your "Please select the area" message.
It's working.
My issue is resolved now. Thank you very much!