Skip Navigation

[Resolved] Date comparison error with Tribe Events Calendar after update

This support ticket is created 6 years, 1 month 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 3 voices.

Last updated by willemP 6 years, 1 month ago.

Assisted by: Nigel.

Author
Posts
#1116145

I'm usings views to generate list of forthcoming events.

In query filter I used following:
Select items with field: _EventStartDate is a DATE greater than or equal TODAY()

After updating Toolset, Views and Events Calendar this comparison is no longer working. It displays all the events.

It is working when I change filter to manual date:
Select items with field: _EventStartDate is a DATE greater than or equal 2018-09-27 00:00:00

I have tried to change DATE to string or number, but result is not correct.
Can you help me to fix this date comparison to work correctly again.

#1116301

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

I went back and checked on a test site with older versions of the Events Calendar and Toolset plugins, and found that, yes, it used to work, but it was actually a fluke that it worked.

The Events Calendar hijacks queries of events posts and adds its own conditions to only display future events.

So this would have been happening with the View on your old site, even if you didn't add that Query Filter for the date yourself.

Now, our last Views update included a change to prevent the Events Calendar from interfering with Views which query event posts (because we had other clients trying to, for example, create a View of past events).

Which means that you must add a Query Filter to achieve what was happening before anyway.

Except... Types date fields are stored as timestamps, and so when you create a Query Filter and use TODAY(), the value for today is also a timestamp, so that like is being compared with like.

But the Events Calendar stores its dates in a format like 2018-09-20 17:00:00, which means you are no longer comparing like with like.

The solution is to modify the query using the wpv_filter_query API hook (hidden link).

You can add the following code to your site (since Types 3.1.1 you can add this at Toolset > Settings > Custom Code so that you don't need to edit your theme's functions.php):

<?php
/**
 * Filter event query to show future events
 */
function tssupp_filter_event_query( $view_args, $view_settings, $view_id ){

  if ( ! isset( $view_args['meta_query'] ) ) {

    $view_args['meta_query'] = array();
  }

  $meta_query = array(

    'key'     =>  '_EventStartDate',
    'value'   =>  date("Y-m-d H:i:s"),
    'type'    =>  'NUMBER',
    'compare' =>  '>'
  );

  $view_args['meta_query'][] = $meta_query;
  
  return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_filter_event_query', 101, 3 );
#1117078

Thank You. This fixes the problem on these Event Calendar views.

But as a side effect, it seems to clear other date-related views (no items showing). How do I make this affect only on views, where items are picked from Event Calendar events.

#1117132

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry, I didn't include any test for which View needs to be modified.

The updated code would be:

<?php
/**
 * Filter event query to show future events
 */
function tssupp_filter_event_query( $view_args, $view_settings, $view_id ){

  if ( in_array( $view_id, array( 99, 123 ) ) ) { // Edit view IDs

    if ( ! isset( $view_args['meta_query'] ) ) {
   
      $view_args['meta_query'] = array();
    }
   
    $meta_query = array(
   
      'key'     =>  '_EventStartDate',
      'value'   =>  date("Y-m-d H:i:s"),
      'type'    =>  'NUMBER',
      'compare' =>  '>'
    );
   
    $view_args['meta_query'][] = $meta_query;

  }
 
  return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_filter_event_query', 101, 3 );

You need to specify the IDs of the Views which should be modified.

#1118167

That ID-filter fixed the issue. Views are now working. Thank you!

#1121485

I have the same issue but after adding this code to functions.php it still is not working. The view keeps showing all dates (meaning the past dates also)

I use this filter: _EventStartDate is a DATE greater or equal to TODAY()