• Resolved purag

    (@purag)


    Hi Events Calendar team,

    Long-time plugin user! Thanks for such a fantastic product. ??

    I am experiencing the following bug. For some brief context: I’m using WPBakery’s post grid component to display tribe_events.

    The post grid component can be fed a custom query in the form of URL-encoded WP_query params.

    I’m using the following custom query:

    post_type=tribe_events&posts_per_page=4&post_status=publish&ends_after=now&orderby=event_date&order=ASC

    To get the next 4 upcoming events. This is working great! Except when there aren’t any upcoming events. When there are no upcoming events, it returns the 4 earliest events (because it’s sorted by event_date with order ASC).

    This seems like a bug in the plugin to me. Note that I can’t use tribe_get_events because the post grid component I’m using only accepts the URL-encoded WP_query parameters.

    Any ideas here?

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Support tristan083

    (@tristan083)

    Hi @purag ,

    Thank you for reaching out.

    I suspect this is due to the latest past events functionality of the plugin. Try disabling the latest past events functionality (see Disabling “Latest Past Events”), and see if that rectifies it.

    Thread Starter purag

    (@purag)

    Thanks for the quick response, @tristan083. I installed the Tweaks plugin and disabled the “Recent Past Events” in the Tweaks menu in Events > Settings. Unfortunately this does not fix the problem.

    I think if this were the problem, I would see the 4 latest past events, but I’m actually seeing the 4 earliest past events.

    • This reply was modified 9 months, 2 weeks ago by purag.
    Thread Starter purag

    (@purag)

    By the way, I also dug through the code for tribe_get_events to see if maybe there are some other “secret” query params I can use, but couldn’t find anything. I tried switching &ends_after=now&orderby=event_date&order=ASC out for eventDisplay=upcoming (which the source code indicated is still supported for backwards compatibility), and the result was similar, but the events seemed to be ordered randomly instead of earliest onward.

    Plugin Support masoodak

    (@masoodak)

    Hi @purag,

    With tribe_get_events() you can use 'start_date' => 'upcoming' to fetch upcoming events only if available and return empty array if none. Here’s an example,

    tribe_get_events( array('start_date' => 'upcoming') );

    Alternatively, you could try the following to get a similar outcome,

    tribe_get_events(array('ends_after' => 'now'));

    Let us know if this helps.

    Thread Starter purag

    (@purag)

    Thanks @masoodak, but I can’t use tribe_get_events in this situation (see above). Will start_date=upcoming work in a wp_query?

    • This reply was modified 9 months, 2 weeks ago by purag.
    Plugin Support masoodak

    (@masoodak)

    Hi @purag,

    The following should help get events starting after now (current time) using WP_Query,

    $args = array(
    'post_type' => 'tribe_events',
    'meta_key' => '_EventStartDate',
    'meta_value' => date( 'Y-m-d H:i:s' ),
    'meta_compare' => '>='
    );

    $query = new WP_Query( $args );

    Let us know if this helps.

    Thread Starter purag

    (@purag)

    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.

    Plugin Support masoodak

    (@masoodak)

    Hi @purag

    Thank you for sharing the detailed workaround. It will be helpful for others aiming for a similar outcome.

    As for querying events that end after now, I have tested the following, and it works as expected. For instance, no results are returned if there are no events ending in the future.

    $args = array(
    'post_type' => 'tribe_events',
    'meta_key' => '_EventEndDate',
    'meta_value' => date( 'Y-m-d H:i:s' ),
    'meta_compare' => '>='
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
    $query->the_post();
    }
    }

    Please let me know if the result is different on your end, or if there is something I am missing that’s needed to trigger the issue.

    Plugin Support Darian

    (@d0153)

    Hi there,

    It seems like this thread has been quiet for a bit, so we’ll go ahead and mark it as resolved. However, if any further questions or concerns come up, please don’t hesitate to start a new thread.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘wp_querying for upcoming events fails when there are no upcoming events’ is closed to new replies.