Removing upcoming events if they have started

Home Forums Calendar Products Events Calendar PRO Removing upcoming events if they have started

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1333325
    PHILLIPA
    Participant

    We have reused the bottom code block – which works fine. If an event over days has started it removes it from the listings. The thread suggested that a test for $query->is_main_query() should be included:

    have tried this but it stops working
    if ($post_type == 'tribe_events' && $query->is_main_query() )

    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 );
    #1333664
    Shelby
    Participant

    Hey Phillipa,

    Thanks for reaching out, I’m happy to help. 🙂

    Based on the code you pasted, it should work as described without the modification I think. Could you double check your timezone settings (both WP and for our plugin) and let me know if they’re aligned with one another?

    Let me know, and we’ll work on this from there!

    Best,

    Shelby 🙂

    #1333930
    PHILLIPA
    Participant

    Hi Shelby

    We found the code here:
    https://theeventscalendar.com/support/forums/topic/filter-events-with-start-date-after-now/

    On the tail end of this Brian suggests that $query->is_main_query() should be included to avoid problems with other views.

    The code block is working fine – I’m just a bit unsure how to incorporate $query->is_main_query()

    Thank you

    #1335222
    Shelby
    Participant

    Hi there,

    I’ve added it into the code below so that you can copy and paste. 🙂

    //All the events must start after the date we filter by
    function start_date_strict($query) {
     $query->is_main_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 );

    Please let me know if you need anything further on this!

    Best,

    Shelby 🙂

    #1344940
    Support Droid
    Keymaster

    Hey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.

    Thanks so much!
    The Events Calendar Support Team

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Removing upcoming events if they have started’ is closed to new replies.