Skip Navigation

[Resolved] Views Query Filter for Custom Field Date in String Format

This support ticket is created 4 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
- 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 2 replies, has 2 voices.

Last updated by davidL-7 4 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#1631049

Hi,

Our website at hidden link is using the Events Manager plugin to handle event listings; we're trying to create a view that shows upcoming events, so we need a query filter that only pulls posts who have an _event_start_date in the future.

Unfortunately, Events Manager stores that _event_start_date as a string (so the value for an event starting today would be 2020-05-19), and I don't think it's possible to filter right in the Views interface because any comparison would be against a timestamp. Is that correct?

So I'm thinking I need to add a function that uses a hook to filter the view, converts the string to a timestamp, and then compares against TODAY() to see if the _event_start_date has passed. If that's the case, could you provide me with some guidance on how to right that function?

Thanks in advance,
David

#1631413

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Views offer the filter wpv_filter_query which you can use the filter view's query on the fly.
=> https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

You can add the following code to the "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/
Or
To the functions.php file of your theme:

function func_filter_tribe_future_events( $view_args, $view_settings, $view_id ){
  
$target_view_ids = array(99999);

  if ( in_array( $view_id,$target_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', 'func_filter_tribe_future_events', 101, 3 );

Where:
- Please replace 99999 with your original view ID.
- You can target the meta key _EventStartDate or _EventEndDate as per your requirement

Replace 12345 with the numeric ID of this View, or a comma-separated list of numeric IDs.

#1632365

Thanks Minesh, I would have never figured that out without your help! I also wasn't aware of the ability to add custom code right through the Toolset interface... I added the PHP snippet that way and it works great!

Thanks again for the support,
David