shortcode – set_default_url_params
Sometimes we need to set default parameters for a view with search filters. Search filters uses URLs parameters, by default, when we load the page with the view, the page doesn’t load any parameter, so the view displays all the results.
It is possible to set default parameters by a shortcode where we can define th default value of each paramenter and the view will GET them has URL parameters.
// Set default url parameters shortcode
add_shortcode('set_default_url_params', 'my_set_default_url_params');
function my_set_default_url_params($atts) {
$atts = shortcode_atts( array(
'param1' => 'wpv-wpcf-year',
'value1' => date("Y"),
'param2' => 'wpv-wpcf-month',
'value2' => date("m")
), $atts);
if(!isset($_GET[$atts['param1']])){
$_GET[$atts['param1']] = $atts['value1'];
}
if(!isset($_GET[$atts['param2']])){
$_GET[$atts['param2']] = $atts['value2'];
}
}
It is just necessary to add this shortcode anywhere in the view or in the page.
[set_default_url_params]
