[Resolved] How to show no initial results on a View until a Filter is set
This thread is resolved. Here is a description of the problem and solution.
Problem:
A View displays *all* results until a filter is applied. How to initially show *no* results?
Solution:
You need to use the Views API and check whether any filters are being applied, and if not, break the query. An example of such code is:
/**
* 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 );
Here is a general purpose function you can use for any View to not display any results until a search filter has been set (a custom field filter, taxonomy filter, or text search filter).
/**
* 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 should just need to edit the $target_views line so that the array contains the IDs of both Views, e.g. array( 123, 456 ).
thank you very much. Is it possible to define a default category / filter, which is loaded by default instead of not displaying or displaying all results?
Not with that code, which is intended to display no results unless any filter is being applied, so if you added a default filter it would break it.
But, then, if you want a default filter to be applied you don't need the above code at all, because, well, the initial results will reflect the default filter.
So if the question really is "how do you apply a default value for a custom search filter?" then you would need to use a different API filter, namely wpv_filter_query, where you intercept the View arguments before the query is submitted, and would check if there is already a value set for a particular filter (a meta_query or tax_query) and if not add the default.
thanks for your explanation. Yes, what you describe would be the way I would need it. Main problem is the following: At the default initial page load I get all results of the second view, which displays several shortcodes at one time, but only one shortcode at one time is needed.
Have you got any example how to use wpv_filter_query as it seems to be a very common case, isn´t it?
thanks again. It seems to work partly, because it doesn´t load anything at the second view. I´ve checked the taxonomy settings at the posts and it should display something at the second view also, while the first view works.
With only one array at your code it does work. If you´ve got any idea, how to do it with two arrays, it would be great. Else I can work with the current code.
I just noticed you added two taxonomy filters, and the default comparison is AND, so only posts will be shown (in either the first or second View) that have *both* of these taxonomy terms assigned.
Is that what you want? Or did you want an *OR* comparison?
If so you would need to update the code like so:
function tssupp_default_taxonomy( $query_args, $view_settings, $view_id ){
if ( in_array( $view_id, array( 357, 353 ) ) ) { // Edit comma-separated list of View IDs
// is there an existing taxonomy query?
if ( ! isset( $query_args['tax_query'] )) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'art',
'field' => 'slug',
'terms' => 'blue', // Edit the correct slug
'operator' => 'IN'
),
array(
'taxonomy' => 'teilnehmerzahl',
'field' => 'slug',
'terms' => 'low-priority', // Edit the correct slug
'operator' => 'IN'
)
);
}
}
return $query_args;
}
add_filter( 'wpv_filter_query', 'tssupp_default_taxonomy', 101, 3 );