• Resolved randyabidin

    (@randyabidin)


    I am looking to use The Events Calendar for a customer's website.
    I am a custom plugin developer.
    
    I am using the tribe_get_events() function with example from:
    https://theeventscalendar.com/knowledgebase/k/using-tribe_get_events/
    
    I am trying to toggle between current/future events and past events.
    However, the code I used below does not display events that started in the past and ends today or in the future.
    It successfully shows events that start & end in past or in the current/future.
    
    I am using the following code:
    
    $filter=$_REQUEST['filter']; // a parameter sent to the script
    
    switch ( $filter ) {
    case "past":
    $key = 'end_date';
    $value = 'now';
    $order = 'DESC';
    break;
    
    default:
    $key = 'start_date';
    $value = 'now';
    $order = 'ASC';
    }
    
    $aa_events_listing = tribe_get_events( [
    'posts_per_page' => 9999, // I want all the events that match
    "$key" => "$value",
    'order' => "$order",
    ] );
    
    I also tried changing $value = 'today'; but no success.
    
    Any help is greatly appreciated!
    Randy
    • This topic was modified 1 year, 5 months ago by randyabidin.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @randyabidin,

    Thanks for reaching out to the Events Calendar support forum.

    The code you’re using is correct, but it’s not filtering the events correctly.

    The end_date key will only return events that have ended on or before the specified date.

    To get events that started in the past and end today or in the future, you need to use the start_date key.

    Here’s the updated code:

    $filter = $_REQUEST['filter']; // a parameter sent to the script
    
        switch ($filter) {
            case "past":
                $key = 'start_date';
                $value = 'now';
                $order = 'DESC';
                break;
    
            default:
                $key = 'start_date';
                $value = 'now';
                $order = 'ASC';
        }
    
        $aa_events_listing = tribe_get_events(array(
            'posts_per_page' => 9999, // I want all the events that match
            "$key" => "$value",
            'order' => "$order",
        ));

    This code will now correctly filter the events so that only events that started in the past and end today or in the future are displayed.

    Let us know how it goes.

    Thread Starter randyabidin

    (@randyabidin)

    Thanks for your quick response, Cheska!

    I changed the $key = ‘start_date’; for the ‘past’ case – but it gives me only the current/future events for the ‘past’ case.

    Thread Starter randyabidin

    (@randyabidin)

    I returned the past case parameter to $key = ‘end_date’;

    I found the parameter setting ‘ends_after’ and put that in for the current/future case and it worked!

    Below is my working code.

    Is there a comprehensive list of all the standard TEC parameters (i.e. start_date, end_date, end_after, etc…)?

    Thank you again for your help!
    Randy

    switch ($filter) {
    
            case "past":
    
                $key = 'end_date';
    
                $value = 'now';
    
                $order = 'DESC';
    
                break;
    
            default:
    
                $key = 'ends_after';
    
                $value = 'now';
    
                $order = 'ASC';
    
        }

    Hi @randyabidin,

    Thanks for letting me know that you’ve resolved the issue.

    I’m glad to hear that you were able to find the solution.

    If you have any more questions in the future, feel free to reach out.

    I will now proceed to close this ticket. Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Event not showing if started in the past & ends today or in the future’ is closed to new replies.