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