Skip Navigation

[Resolved] Don’t show any results until filter is applied

This support ticket is created 3 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 17 replies, has 4 voices.

Last updated by Christian Cox 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1905019

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: hidden link

I found a code here: https://toolset.com/forums/topic/dont-show-any-results-until-filter-is-applied/
But it didn't worked for me.

#1905341

Nigel
Supporter

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

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

Are you using legacy Views or Blocks?

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:

	<!-- wpv-loop-start -->
[wpv-conditional if="( '[wpv-search-term param='wpv-wpcf-kanton-deutsch']' ne '' )"]
	<wpv-loop>
		[wpv-post-body view_template="loop-item-in-classic-search-things"]
	</wpv-loop>
[/wpv-conditional]
	<!-- wpv-loop-end -->

You don't need to use PHP for this.

#1906015

Hello Nigel

Thanks, that sounds good, but I am using the Blocks View. Can you tell me which values I have to choose in the Conditional Block?

Regards
Christoph

#1906115

Nigel
Supporter

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

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

Screenshot 2021-01-15 at 07.50.26.png

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.

#1906269

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?

#1906349

Nigel
Supporter

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

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

Screenshot 2021-01-15 at 07.49.07.png

Insert a Conditional Block.

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.

#1906439
screenshot-3.png
screenshot-2.png

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.

#1906495

Nigel
Supporter

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

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

It looks like it should be working.

Do you not have a staging site for testing?

Let me set up a private reply to get site credentials from you so that someone can log in to your site and take a look.

You may want to create a temporary admin user for me to use that you can later delete. And be sure to have a current backup of your site.

#1906697
Screen Shot 2021-01-15 at 10.30.32 AM.png

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 );
#1908767

Hi Christian

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.

Regards
Christoph

#1909843

Shane
Supporter

Languages: English (English )

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

Screenshot 2021-01-18 at 3.56.27 PM.png

Hi Christoph

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

#1910203
screenshot.jpg

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)?

For example here: hidden link

Regards, Christoph

#1910799

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 hidden link. 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?

#1910841

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...

Regards
Christoph

#1911059
Screen Shot 2021-01-19 at 1.18.51 PM.png

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.