Skip Navigation

[Resolved] No-date-set option for date filter on view

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

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by matthewC-9 5 years, 5 months ago.

Assisted by: Luo Yang.

Author
Posts
#1254603

Tell us what you are trying to do?
I am making a view of a content type called a feature. Features have an optional start and end date. I want to set up the view so that features start showing on their start date and stop showing after their end date. However, if no start / end date is set, I want the feature to show.

In my view, I have set up this query filter which basically handles the start / end date part the way I want (although it may be causing some performance problems - my sample page displaying this view won't always load for me) :

Select items with field:
Feature area is a number equal to VIEW_PARAM(area)
AND
Start date is a number lower than or equal NOW()
AND
End date is a string greater than NOW()

But I'm not sure how to do the also-show-if-no-date-set part. What's the best way to go about this? (I'm happy to do something other than the query filter I have set up if there's a better option.)

#1254701

Hello,

I assume we are talking about you are going to display posts which start and end date field values are not NOT EXISTS.
If it is, it needs custom codes, for example:
1) Use filter hook wpv_filter_query to trigger a PHP function:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

2) Add filters to query, custom fields "start and end date" not NOT EXISTS, see WordPress document:
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
meta_compare (string) - Operator to test the 'meta_value'. Possible values are ... 'NOT EXISTS'

#1261607

Thanks, Luo, the WP documentation was helpful. If anyone finds this looking to do something similar, I ended up adding this as a code snippet under Toolset > Settings > Custom Code, and it seems to be working correctly:

add_filter( 'wpv_filter_query', 'check_visible_dates', 99, 3 );
function check_visible_dates( $query_args, $view_settings, $views_id ) {
  if ( $views_id == 126 ) {
    $query_args['meta_query'][] = array(
      'relation' => 'OR',
      array(
              'key'     => 'wpcf-end-date', 
              'compare' => 'NOT EXISTS',
        ),
      array(
              'key'     => 'wpcf-end-date', 
              'value'   => current_time('timestamp'),
              'compare' => '>',
        ),
    );
    $query_args['meta_query'][] = array(
      'relation' => 'OR',
      array(
              'key'     => 'wpcf-start-date', 
              'compare' => 'NOT EXISTS',
        ),
      array(
              'key'     => 'wpcf-start-date', 
              'value'   => current_time('timestamp'),
              'compare' => '<=',
        ),
    );
  }
  return $query_args;
}