I have a custom search View with one dropdown filter. I would like to hide all the results in the View until at least one filter has been applied. The page is here: enlace oculto
In either case the simplest solution is to wrap the output of the View in a conditional shortcode that checks whether a search parameter for your filter has been applied (and only show the results when there such a parameter *is* present, i.e. a filter has been applied).
In the case of a legacy View it would look something like this in your case:
With the Conditional Block it doesn't allow for adding parameters using the UI so you have to edit the condition manually.
You can use the UI to insert the search term Views shortcode with the condition that it should != nothing, and then switch to advanced mode to add the param attribute, so that you end up with this:
( ( '[wpv-search-term param='wpv-wpcf-kanton-deutsch']' ne '' ) )
You need to make sure that you add the Conditional Block at the end of but within the View block, and then you drag and drop the View output section into the Conditional Block. If you are not used to dragging and dropping blocks like this it can be a little tricky, be sure to save your page before you try so you can revert if you mess things up. The structure should then be as per the screenshot with the View output section within the conditional block.
Thanks, somehow I don't see how to insert the shortcode - which block do I use for this (I don't know exactly what you mean by "use the UI to insert the shortcode)? Can I just a html-block to add the Code? And where can I switch to an advanced mode?
It will show a dialog to specify the conditions as shown in this screenshot, where you can choose Views shortcode in the first dropdown and then opt for the search term shortcode. Because the search term shortcode needs a param attribute specifying but you cannot insert that via the UI, you need to switch to advanced mode (there is an Advanced Editor toggle) to add it. You can skip choosing the shortcode from the dropdown and go straight to the advanced editor and then paste in the actual condition which I shared above.
Ok, I did that. On the frontend, the filter is shown without results. But when I choose something in the dropdown, nothing changes, no results get displayed. See screenshots...
I have to change it back as it was before, because it is a live page.
Hi, I can see why the custom code was not working as expected. The custom code was written to hide results if no filters are applied, but your View has one filter applied in the Query Filters panel - a taxonomy filter for the Marken taxonomy. Screenshot attached. I have modified the custom code to handle this case, and now the View shows no results found until some filter is selected. Can you confirm?
New custom code:
// Add custom Theme Functions here
function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){
$target_views = array( 185905,456,188864 ); // Comma-separated list of View IDs
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['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 );
Thanks a lot, so that can't be done with a Conditional Block right? The code works fine for me, just one more question: Is it possible to write a custom text instead of "No items found". The page is in German and French and I have no WPML installed. So it would be handy to add a custom text when no items are found.
Christian is currently on holidays today but will be back tomorrow to continue assisting with this one.
However checking on the issue here you mentioned "Is it possible to write a custom text instead of "No items found". "
Yes it is possible to have a different text from the No Items Found. Given that you are using the blocks setup, you will need to click on the view loop using the block navigation tool.
Then on the block sidebar under Loop Template you should see the No Items Found text. See Screenshot.
Thanks Shane, that works fine. One thing though - when there are a lot of results, the pagination is shown, even if there are no results, that looks very strange. Is there a way to remove the pagination, when first loading the page (see screenshot)?
Hi, sorry for the delay I was unavailable yesterday. I see what you mean about pagination, and I seem to recall I used a conditional block to solve this problem in the test page enlace oculto. My memory isn't quite clear though, so I tried to log in and confirm this but the login credentials you previously provided are no longer functioning. Would you like to provide updated credentials so I can confirm that, and give you some guidance for implementing a similar conditional?
Hello Christian - I created your account with the same data again. Thanks for having a look. I think the problem isn't solved, because on the test page, there are 18 data sets in this category, and the limit to display on the page is 24. You have to lower this limit to see the pagination...
Okay thank you, I have added a conditional block in the test page. The conditional is configured to test whether the View returns more than 24 results (i.e. the current number of posts per page in the View's pagination configurations). Then I dragged the Pagination block inside the Conditional block to implement the conditional display of pagination. See the screenshot attached here for more information about these configurations in the Conditional block. I hope this is clear.
If the test page is now functioning as you would expect, you can implement a similar conditional block in the Daimants BZQ page. Would you like to try to implement this yourself? I think it will help you understand how to use the conditional feature in Blocks. This is a powerful feature you may need to use again in the future.