Skip Navigation

[Resolved] Setting a default value for a view being passed a URL parameter

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 0 replies, has 1 voice.

Last updated by Saul Baizman 1 day, 1 hour ago.

Assisted by: Minesh.

Author
Posts
#2795353

Hi there,

We've set up a view to display a list of custom posts that have been published in a given year. The year is supplied to the view via a URL parameter.

When there is no URL parameter present, we want to be able to set a default value (the current year). Is this possible to do in the dashboard interface? If not, is it possible via custom PHP code?

Thank you for your help.

Saul

#2795370

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I'll have to review your current view settings. Once I go through all those configuration I will be able to guide you in the right direction.

Can you please share problem URL where you added your view as well as admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2795553

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now: hidden link

I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link

We use the view's filter hook "wpv_filter_query" to modify the view's query on fly and to exclude the required post:

add_filter('wpv_filter_query', 'func_show_current_year_post_by_default_for_post_date', 101, 3);
function func_show_current_year_post_by_default_for_post_date($query_args, $view_settings, $view_id) {

  $target_view_ids = array(20058);
  
  if ( in_array( $view_id, $target_view_ids ) and !isset($_GET['prog-year']) ) {
    
    $getdate = getdate();
$query_args['date_query'] = array(array(
            'year'  => $getdate["year"]));
    
  }
  return $query_args;
}

More info:
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#benefits-of-adding-custom-code-using-toolset

#2795594

Minesh,

This works beautifully! Thank you for your PHP magic!

Saul