Skip Navigation

[Resolved] set view default value if views is not set

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to have a default search filter value if none is selected by the user, e.g. on the initial page load?

Solution:
You can use the Views API filter wpv_filter_query to modify the underlying query so that if no filter value has been specified then the default is set. The code becomes slightly more complex if you need to allow for the possibility that there are other filters available in the same search that may or may not have been set aside from the filter to which you wish to apply a default.

An example of such code that needs editing for your site appears below: https://toolset.com/forums/topic/set-view-default-value-if-views-is-not-set/#post-1314497

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 3 years, 5 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
- 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)

This topic contains 4 replies, has 2 voices.

Last updated by culturaI 3 years, 5 months ago.

Assigned support staff: Nigel.

Author
Posts
#1313967

Hello,
i have a RFG with slug: ripetibile-asoip
i have in this RFG a custom field: anno-corso, 2 possible values: 2019 - 2020
i have a view that output correctly my RFG.

Everything is good until now.

Obviously when visitors visit my page the see all my rfg 2019+2020

I would to filter my view by default to 2019 only when they come to my page, i.e. when the view is not still set.

I know that i could to use url parameter, but i have multiple views on my site, so i would prefer to use wpv_filter_query hook (if it's the better way to proceed).
I tried to look in this forum and documentation but I have not been able to reach my goal.

Can you help me to achieve that?

Thanks

#1314165

Nigel
Supporter

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

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

Hi there

This RFG belongs to some post type, so I assume that the page your visitors are loading is a single post of that type, and then the View shows the RFG entries for that post, is that correct?

And initially when the page is loaded, you only want to show the fields from the group where anno-corso = 2019.

I can explain how to do that using the wpv_filter_query hook, but I'm wondering how and when you intend to display the 2020 fields?

You want to display 2019 fields when the page first loads, but how should users switch to 2020 if not by using a custom search (which will add a URL parameter).

If you *are* using a custom search with URL parameter, but you just want to display only 2019 if no parameter exists I can show you that, but I just want to make sure I understand the question first.

#1314187
view homepage.jpg

Hello Nigel and thanks for your help.

You understood well.
However for explaining better , i have 2 types of views:

1 - Home page - hidden link - i'm displaying RFG child of a selected course from another view (you can look in attachment). I'm using field wpcf-anno-corso to filter various views, as other support ticket resolved here: (https://toolset.com/forums/topic/filter-repeteable-group-fields-using-views/)

2 - post type CPT courses, other view displaying RPG child of current post. I resolved autoselect of anno-corso using a shortcode - hidden link

My hope was to using a single snippet to manipulate output of all views in the site (with anno-corso filter) when users arrive on the various pages, for auto set field anno-corso to 2019. They must however have the possibility to select 2020 after.

I hope that is clearer

#1314497

Nigel
Supporter

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

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

This sounds like a similar request to wanting to show no initial results on a search page until a search filter had been selected, only in your case you want to show results defaulting to a particular search term if no filter has been selected.

So you'll need to use the wpv_filter_query filter to modify the View query arguments so that if there is no filter already specified then the default will be applied. You can apply this to as many Views as is necessary (edit the array of View IDs).

The code is a little complex because you need to allow for the case where no filters have been specified (and you add a meta_query argument) and the case where you have filters applied, but not the filter you want to force the default value for (in which case you update the existing meta_query argument).

/**
 * Default filter value when no value specified
 *
 */
function tssupp_default_filter_value( $view_args, $view_settings, $view_id ){
  
	$target_views = array( 123, 456, 315 ); // Edit to add IDs of Views to apply this to

    $field = 'wpcf-anno-corso'; // keys of Types fields have a 'wpcf-' prefix
    $default = '2019';

    if ( in_array( $view_id, $target_views ) ) {

        if ( isset( $view_args['meta_query'] ) ){

            $keys = array_column( $view_args['meta_query'], 'key' );

            if ( !in_array( $field, $keys ) ){
                $view_args['meta_query'][] = array(
                    'key' => $field,
                    'value' => $default,
                    'type' => 'CHAR',
                    'compare' => '='
                );                
            }
        } else {
            $view_args['meta_query'] = array(
                'relation' => 'AND',
                array(
                    'key' => $field,
                    'value' => $default,
                    'type' => 'CHAR',
                    'compare' => '='
                )
            );
        }
    }
  
    return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_default_filter_value', 101, 3 );

I tested that on my site and it seems to do what is required, but you had better test and confirm for yourself.

#1315101

The code is working great.
My issue is resolved now. Thank you Nigel!