Events are in WP Search results but I only need future events to display

Home Forums Calendar Products Events Calendar PRO Events are in WP Search results but I only need future events to display

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #151953
    User Services
    Participant

    I’m looking for help with integrating event calendar events with built-in WordPress search.

    I’ve got as far as all events that match the query are displayed. However, I’d rather exclude past events in search results. I’ve seen this page: https://tri.be/support/forums/topic/displaying-events-in-search-or-category-or-taxonomy-results-pages/ but I do not yet understand how to modify the code I currently have to incorporate the date filtering in the search results.

    If it helps, here is the code I’m using:

    #152119
    Barry
    Member

    Hi – great question!

    There are a few ways you might go about this and I think filtering on pre_get_posts as Jonah suggested in the other thread you linked to would probably be the cleanest approach, particularly in terms of supporting pagination where a large number of results are returned.

    It looks like the approach you are using is more template driven which is totally fine if it meets your needs and certainly is likely to be a fast and robust way to do things in many cases. With that in mind, perhaps you could add a condition to ignore results that expired before the current date and time?

    if ( tribe_get_end_date( null, false, 'Y-m-d H:i:s' ) < date( 'Y-m-d H:i:s' ) ) continue;

    Something like the above might work though probably needs some refinement.

    Does that help at all?

    #152816
    User Services
    Participant

    Thanks, Barry!

    I added your code to the php else clause for the events and now only current and future events display in the search results.

    Thanks so much!

    #152831
    User Services
    Participant

    Barry,
    I may have spoke too soon. Upon closer inspection the events are being filtered by date but they now show the date and time of the search instead of their original start date/time. Is the if statement effecting the tribe_get_state_date() inside the result?

    #152847
    User Services
    Participant

    Barry, thank you again for the direction! I believe I’ve got what I need now. I swapped out the tribe_get_start_date() for the_time(get_option(‘date_format’)) and I’m now seeing filtering and the correct start date on events in search.

    #154125
    Barry
    Member

    Ahh ok 🙂 glad you figured things out!

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Events are in WP Search results but I only need future events to display’ is closed to new replies.