Okay, thanks for the tip @masoodak. Unfortunately in the URL string version (which is all the WPBakery element accepts), I can’t get the current date using PHP date(...)
.
However, I went digging through the WPBakery source code, and found that they actually filter the WPQuery string before executing it. Using that filter (vc_basic_grid_filter_query_filters
), I was able to implement a “custom” query parameter that then is translated to the date filtering I desired. I also implemented a custom parameter to filter by event organizer (this is in functions.php
):
add_filter('vc_basic_grid_filter_query_filters', 'vc_basic_grid_tribe_events_hwr_filter', 10, 3);
function vc_basic_grid_tribe_events_hwr_filter($query, $atts, $shortcode) {
// Parse the string out to an associative array
parse_str($query, $filtered_query);
// Only apply translations on tribe_events post type.
if ($filtered_query['post_type'] == 'tribe_events') {
// If our custom param indicates to filter to upcoming, apply the meta_query.
if ($filtered_query['hwr_events_filter'] == 'upcoming') {
if (!isset($filtered_query['meta_query'])) {
$filtered_query['meta_query'] = array();
}
$filtered_query['meta_query']['relation'] = 'and';
$filtered_query['meta_query'][] = array(
'key' => '_EventEndDateUTC',
'value' => date('Y-m-d H:i:s'),
'compare' => '>=',
'type' => 'DATE',
);
unset($filtered_query['hwr_events_filter']);
}
// If the hwr_event_organizer custom param is set, apply the meta_query.
if (!empty($filtered_query['hwr_events_organizer'])) {
if (!isset($filtered_query['meta_query'])) {
$filtered_query['meta_query'] = array();
}
$filtered_query['meta_query']['relation'] = 'and';
$filtered_query['meta_query'][] = array(
'key' => '_EventOrganizerID',
'value' => $filtered_query['hwr_events_organizer'],
'compare' => '=',
'type' => 'NUMERIC',
);
unset($filtered_query['hwr_events_organizer']);
}
}
// Translate the associative array back into the URL-encoded string.
return html_entity_decode(http_build_query($filtered_query), ENT_QUOTES, 'utf-8');
}
This is working excellently. Although, it still has a problem if there aren’t any upcoming events — it shows an infinite spinner where the grid is supposed to be. Though I think that’s not a problem with The Events Calendar but instead with WPBakery, so I’ll reach out to them regarding that bug.
Hopefully this turns out useful for other folks in the future.
Again, in WPBakery’s Post Grid element, you can only pass a URL-encoded string version of the WP Query. So if you put the above code in functions.php
, then in WPBakery’s post grid settings, you can set the Data Source to “Custom Query” and use the following:
post_type=tribe_events&posts_per_page=4&post_status=publish&orderby=event_date&order=ASC&hwr_events_filter=upcoming
You could also add &hwr_events_organizer=<organizer ID #>
to filter to specific organizers. You get the organizer ID by going to Events > Organizers in the admin UI, clicking the organizer you’re interested in, and grabbing the post ID from the URL.
@masoodak @tristan083 I still believe there is a bug in The Events Calendar, though. If I’m using ends_after=now
in the query and there are no upcoming events, it should just return an empty result, not show any previous events for which ends_after=now
doesn’t apply. Can we continue looking into that? The above is just a workaround.