Filter events with start date after now

Home Forums Calendar Products Events Calendar PRO Filter events with start date after now

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #967745
    Pablo
    Participant

    Hello supporters,

    I got stuck in a problem that maybe you can help me with.

    In the solution I am working with, we have events from every kind. We have the community plugin, so people insert the events by themselves, and it is normal that they put a start date of the event in 1st of June and the end date at 27th of June, when they actually shall create a recurrent event instead.

    We want to avoid that behaviour, so we will only show events with start date after “now” (or the date on the filter bar). In order to do that I created this filter:

    //All the events must start after the date we filter by
    function start_date_strict($query) {
    $post_type = $query->query_vars['post_type'];

    if ($post_type == 'tribe_events') {

    $filterDate = current_time('Y-m-d H:i:s');
    if ( !empty( $_REQUEST['tribe-bar-date'] ) ) {
    $filterDate = $_REQUEST['tribe-bar-date'];
    }

    $query->query_vars['meta_query'] = array(
    array( // restrict posts based on meta values
    'key' => '_EventStartDate', // which meta to query
    'value' => $filterDate, // value for comparison
    'compare' => '>=', // method of comparison
    'type' => 'DATETIME')
    );
    }
    }
    add_filter( 'tribe_events_pre_get_posts', 'start_date_strict', 100, 1 );

    The behaviour was the expected, but then I meshed up some other things that were working fine. For example, when I click in a venue to list all the events, the query is completely changed and does not filter by venue anymore (the same with the organizers).

    Is there any other filter more suitable for this purpose? I want it to work mainly on the main loop and also with the ajax filtering (date, category, etc).

    Thanks in advance for your awesome help! 🙂

    #967749
    Pablo
    Participant

    Dear supporters,

    I think I found the problem. I was doing something wrong, since I was overriding the meta_query entirely. The code should be like this:


    //All the events must start after the date we filter by
    function start_date_strict($query) {
    $post_type = $query->query_vars['post_type'];

    if ($post_type == 'tribe_events') {

    $filterDate = current_time('Y-m-d H:i:s');
    if (!empty( $_REQUEST['tribe-bar-date'] ) ) {
    $filterDate = $_REQUEST['tribe-bar-date'];
    }

    $query->query_vars['meta_query'][] =
    array( // restrict posts based on meta values
    'key' => '_EventStartDate', // which meta to query
    'value' => $filterDate, // value for comparison
    'compare' => '>=', // method of comparison
    'type' => 'DATETIME');
    }
    }
    add_filter( 'tribe_events_pre_get_posts', 'start_date_strict', 100, 1 );

    Thanks anyway!

    #967816
    Brian
    Member

    I am glad to see you were able to figure it out.

    Although you should look to adding a main query check:

    $query->is_main_query()

    As your coding could modify more then just that and mess up some of the other views and widgets in the events calendar.

    I am going to go ahead and close this ticket. If you have a similar issue or another in the future, please do not hesitate to create a new ticket.

    Thanks!

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Filter events with start date after now’ is closed to new replies.