Skip Navigation

[Resolved] Datepicker not filtering on start of view to today or greater

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

Problem: I have a View with two custom field filters. The first filter is applied using a shortcode attribute. The second filter is a front-end filter using a datepicker field. I can set the default date using TODAY(), but the initial result is not filtered. I would like to filter the initial results, and allow the User to change the filter as needed.

Solution:
It's possible to filter the initial result set using the default selection TODAY while still allowing front-end date filtering. Add the following code to your child theme's functions.php file, or to a new snippet in Toolset > Settings > Custom Code:

add_filter( 'wpv_filter_query', 'filter_gte_today', 10, 3 );
function filter_gte_today ( $query, $view_settings, $view_id ) {
  $views = array( 12345 );
  $date_slug = 'wpcf-stdstartdate';
  $url_param = 'wpv-wpcf-stdstartdate';
 
  // do not edit below this line
 
  if( in_array( $view_id, $views) ) {
    // check to see if the datepicker filter has been manually set or manually unset
    // if so, bail out and don't modify it
    if( isset($query['meta_query'])) {
      $cols = array_column($query['meta_query'], 'key');
      if( in_array($date_slug, $cols) || ( isset( $_GET[$url_param] ) && $_GET[$url_param] == '' )) {
        return $query;
      }
    }
 
    // datepicker filter has not been set or unset. Use the default value.
    $today = strtotime('0:00');
    $args = array(
      'relation' => 'AND',
      array(
        'key' => $date_slug,
        'value' => $today,
        'compare' => '>=',
        'type' => 'numeric'
      )
    );
    // add these arguments to your meta query
    $query['meta_query'] = isset($query['meta_query']) ? $query['meta_query'] : [];
    $query['meta_query'][] = $args;
  }
 
 return $query;
}

Change 12345 to match the numeric ID of this View. Change wpcf-stdstartdate to match the date field slug. You must use the 'wpcf-' prefix here. Change wpv-wpcf-stdstartdate to match the URL parameter specified in your Query filter.

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

This support ticket is created 5 years, 6 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 5 replies, has 2 voices.

Last updated by mikeB-14 5 years, 6 months ago.

Assisted by: Christian Cox.

Author
Posts
#1280201

Hi

I have a filter set in my view which is sett to filter by default wpcf-stdstartdate >= today
When view shows the datepicker is set to today's date but the row are not filtered by default

If i select the date in the datepicker and then sumbit the rows get filtered.
I would like to have a default set from >= today
The user can choose to change the filter is needed.

My page which has this view is configured this way

[wpv-conditional if="( '[wpv-current-user info='email']' ne '' )"]
[wpv-view name='planner-list-view-with-custom-search-v2' cached="off" orderby='field-stdstartdate' email="[wpv-current-user info='email']"]
[/wpv-conditional]

Thanks Tony

----------------------------------------------------------------------------
[wpv-filter-start hide="false"]
[wpv-filter-controls]
<div class="form-group">
<label>[wpml-string context="wpv-views"] Select Start Date[/wpml-string]</label>
[wpv-control-postmeta field="wpcf-stdstartdate" default_date="TODAY()" url_param="wpv-wpcf-stdstartdate"]
[wpv-filter-submit output="bootstrap"]
[/wpv-filter-controls]
<div class="pagination">
[wpv-pager-prev-page force="true"][wpml-string context="wpv-views"]Previous Page [/wpml-string][/wpv-pager-prev-page]
[wpv-pager-next-page force="true"][wpml-string context="wpv-views"]Next Page[/wpml-string][/wpv-pager-next-page]

</div>
[wpv-filter-end]
</div>

#1280381

Hi, if the datepicker is the only custom field filter in the View, then the following custom code snippet will filter the initial result set correctly:

add_filter( 'wpv_filter_query', 'filter_gte_today', 10, 3 );
function filter_gte_today ( $query, $view_settings, $view_id ) {
  // if the datepicker is already set, don't modify the selected value
  if( isset($query['meta_query'])) {
    return $query;
  }

  $views = array( 12345 );
  if( in_array( $view_id, $views) ) {
    $today = strtotime('0:00');
    $args = array(
      'relation' => 'AND',
      array(
        'key' => 'wpcf-stdstartdate',
        'value' => $today,
        'compare' => '>=',
        'type' => 'numeric'
      )
    );
    // add these arguments to your meta query
    $query['meta_query'] = isset($query['meta_query']) ? $query['meta_query'] : [];
    $query['meta_query'][] = $args;
  }

 return $query;
}

Change 12345 to match the numeric ID of this View, then place this snippet in a child theme's functions.php file or in a new custom code snippet in Toolset > Settings > Custom Code.

#1280681

MY view has 2 filters

Select items with field: Email is a string equal to VIEW_PARAM(email)

AND STDstartDate is a number greater than or equal URL_PARAM(wpv-wpcf-stdstartdate)

#1280709

Hi Christian
I have also added the code snippet into the custom code and activated it , does not appear to doing anything

#1281107

Okay yes if there is more than one custom field filter, the code is a bit more complex. Here's an updated snippet:

add_filter( 'wpv_filter_query', 'filter_gte_today', 10, 3 );
function filter_gte_today ( $query, $view_settings, $view_id ) {
  $views = array( 12345 );
  $date_slug = 'wpcf-stdstartdate';
  $url_param = 'wpv-wpcf-stdstartdate';

  // do not edit below this line

  if( in_array( $view_id, $views) ) {
    // check to see if the datepicker filter has been manually set or manually unset
    // if so, bail out and don't modify it
    if( isset($query['meta_query'])) {
      $cols = array_column($query['meta_query'], 'key');
      if( in_array($date_slug, $cols) || ( isset( $_GET[$url_param] ) && $_GET[$url_param] == '' )) {
        return $query;
      }
    }

    // datepicker filter has not been set or unset. Use the default value.
    $today = strtotime('0:00');
    $args = array(
      'relation' => 'AND',
      array(
        'key' => $date_slug,
        'value' => $today,
        'compare' => '>=',
        'type' => 'numeric'
      )
    );
    // add these arguments to your meta query
    $query['meta_query'] = isset($query['meta_query']) ? $query['meta_query'] : [];
    $query['meta_query'][] = $args;
  }

 return $query;
}

Again, change 12345 to match the View's unique ID.

#1281733

My issue is resolved now. Thank you!