- Using The Events Calendar plugin by Tribe for WP as a community calendar.
- Need to show five upcoming events in a section on the home page.
- Only want to show events that have a start date of today or in the future.
The function get_tribe_events would pull in events that encompassed today’s date but had started in the past. The client only wanted events that had a start date of today or in the future to be displayed to avoid a situation where five long spanning events stayed on the homepage as the next upcoming events.
The problem seemed to stem from the way The Events Calendar was handling reoccurring events (or events that spanned multiple days). It would apply a start date of today’s date if today was encompassed in the date range of the event. Therefore, in a foreach loop when you asked get_tribe_events to only show upcoming events or all events with a start day of at least today it would also include these multi-day events.
I tried a slew of conditional statements in the loop to try and ignore the events whose start date was in the past but I just couldn’t get it quite right, the loop still counted these false-positives and I was continually having to update the posts_per_page argument to prevent there from being too many or too few events. It wasn’t until I start printing a second instance of the start date of an event that I learned the loop was behaving normally but I targeting a fake start date.
From reading around it seems like each event (specifically those that were reoccurring or multi-day) were given two start dates in the database and get_tribe_events was pulling the “wrong” one for use in it’s decision on what was an upcoming event. If an event spanned multiple days it would receive today’s date as it’s start date so the loop would know to continue showing it even if the actual start date of the event was months in the past.
Once I knew that I needed to target a specific row in the database I switched across to a WP_Query to utilize the meta_query argument. I was then able to target the actual start date of the event with _EventStartDate and use compare to tell the query to only display events that occurred on or after today’s date.
Here’s the code I used to solve the problem: