Skip Navigation

[Gelöst] How to show no initial results on a View until a Filter is set

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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

Relevant Documentation:
https://wp-types.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process

This support ticket is created vor 6 Jahren, 4 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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)

Dieses Thema enthält 13 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von Matthias Reichl vor 6 Jahren, 3 Monaten.

Assistiert von: Nigel.

Author
Artikel
#1071424

Hi,

thanks, you´re right. How can I preselect an option to have displayed not all items at the first load?

Regards,

#1072489

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

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

Hi Rainer

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

#1072554

Hi,

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?

Regards,

#1072569

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

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

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.

Does that sound more like what you have in mind?

#1074768

Hi,

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?

Regards,

#1075058

Hi, Nigel will return tomorrow to continue assisting you with this ticket. Thanks for your patience.

#1076169

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

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

Hi Rainer

Here is an example using wpv_filter_query to set a default taxonomy filter if one is not already added by the search form:

/**
 * Apply default taxonomy filter on initial page load
 */
function tssupp_default_taxonomy( $query_args, $view_settings, $view_id ){

	if ( in_array( $view_id, array( 26, 31 ) ) ) { // 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'	=> 'colour',
					'field'		=>	'id',
					'terms'		=>	array( 3 ),
					'operator'	=>	'IN'
				)
			);

		}
	}  

	return $query_args;
}
add_filter( 'wpv_filter_query', 'tssupp_default_taxonomy', 101, 3 );

In my example I have a 'colour' taxonomy if there is no search term set it will by default add a filter for the term ID = 3 ('blue' on my test site).

You will obviously need to modify this to your needs.

#1076177

Hi,

thank you very much. I´ve adapted your code and inserted it into my child´s functions.php. Unfortunatelly it doesn´t work.

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'     =>   'id',
                    'terms'     =>   array( 1 ),
                    'operator'  =>   'IN'
                ),
                array(
                    'taxonomy'  => 'teilnehmerzahl',
                    'field'     =>   'id',
                    'terms'     =>   array( 1 ),
                    'operator'  =>   'IN'
                )
            );
        }
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'tssupp_default_taxonomy', 101, 3 );

Have I to set a higher priority or what could fail?

Regards,

#1076288

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

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

You have used array ( 1 ) for both of the terms you want to set as the default for each taxonomy, which is unlikely to be what you intended.

In my example I'm setting the colour taxonomy filter to filter by the term blue (which has a term ID of 3).

Let's change the code to use the taxonomy slugs instead, which should be easier for you to identify and add, e.g.

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

Hi,

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.

Regards,

#1076365

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

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

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

Hi,

thanks, but it seems to be the same code as posted above?

Regards,

#1076369

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

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

Sorry, pasted the before not the after.

It is a minor change to include the relation parameter.

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'
                ),
                'relation'  =>  'OR'
            );
        }
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'tssupp_default_taxonomy', 101, 3 );
#1076375

Hi,

thanks a lot. Fixed!

Regards,