Exclude past events from WP search

Home Forums Calendar Products Events Calendar PRO Exclude past events from WP search

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1321389
    Sabina Ramsey
    Participant

    I’ve seen a few posts asking about this but it seems that they are all a little too customized to what that user was doing rather than a general way to do this. I see that the code here (https://theeventscalendar.com/support/forums/topic/exclude-events-from-search-results/) does remove events from search, but it removes all events rather than just past ones. How can I do this same thing but only for removing past events?

    #1322311
    Victor
    Keymaster

    Hello Sabina!

    Thanks for reaching out to us!

    You can try the top snippet available here https://theeventscalendar.com/support/forums/topic/exclude-past-events-from-search-results-2/. This worked for the user after removing other search filtering plugins that were ignoring the parameter set there.

    Let me know if that helps.

    Best!
    Victor

    #1322339
    Sabina Ramsey
    Participant

    I tried adding that code with and with the search plugin I had been attempting to use (Relevanssi) deactivated and activated I was getting this error when I clicked to search:

    Fatal error: Uncaught Error: [] operator not supported for strings in /home/sitename/public_html/dev/wp-content/themes/polyclinic-child/functions.php:161 Stack trace: #0 /home/sitename/public_html/dev/wp-includes/class-wp-hook.php(298): edcny_pre_get_posts(Object(WP_Query)) #1 /home/sitename/public_html/dev/wp-includes/class-wp-hook.php(323): WP_Hook->apply_filters(NULL, Array) #2 /home/sitename/public_html/dev/wp-includes/plugin.php(515): WP_Hook->do_action(Array) #3 /home/sitename/public_html/dev/wp-includes/class-wp-query.php(1683): do_action_ref_array(‘pre_get_posts’, Array) #4 /home/sitename/public_html/dev/wp-includes/class-wp-query.php(3248): WP_Query->get_posts() #5 /home/sitename/public_html/dev/wp-includes/class-wp.php(617): WP_Query->query(Array) #6 /home/sitename/public_html/dev/wp-includes/class-wp.php(735): WP->query_posts() #7 /home/sitename/public_html/dev/wp-includes/functions.php(955): WP->main(”) #8 /home/sitename/public_html/dev/wp-blog-header.php(16): in /home/sitename/public_html/dev/wp-content/themes/polyclinic-child/functions.php on line 161

    #1322661
    Sabina Ramsey
    Participant

    Actually sorry scratch that error, I figured out what that was from, but it seems that I’m still seeing some past events show up if I search for their title.

    #1322901
    Victor
    Keymaster

    Hello Sabina!

    Thanks for following up with this!

    I could make the following code snippet work for me:

    add_action( 'pre_get_posts', 'edcny_pre_get_posts' );
    function edcny_pre_get_posts($query) {
    if (!is_admin() && $query->is_main_query()) {
    if ($query->is_search) {
    
    $meta_query = array();
    $meta_query[] = array(
    'key' => '_EventStartDate',
    'value' => date('Y-m-d').' 00:00:00',
    'compare' => '>=',
    'type' => 'DATETIME'
    );
    $query->set('meta_query', $meta_query);
    }
    }
    return $query;
    }
    

    Will this work for you? Let me know about it.

    Best,
    Victor

    #1323466
    Sabina Ramsey
    Participant

    I’ve added that code, tried flushing out the cache and checked again and I am still seeing past events.

    #1323649
    Victor
    Keymaster

    Hello Sabina!

    I’m sorry that doesn’t work for you.

    I do want to note that we are fairly limited in how much we can support custom development questions like this.

    That said, we do like helping out and at least point you in the right direction as much possible. We also have a list of customizers we’d like to recommend for this level of help.

    Are you currently using any plugin that might be customizing the search or perhaps you have some custom code that might be conflicting with this? If so, please try switching that off and see if any difference. Let me know about this.

    When coming back, could you please share with us your system info by following this guide > https://theeventscalendar.com/knowledgebase/sharing-sys-info/ ? This way we’ll see if we can spot the issue from there.

    Thanks,
    Victor

    #1328993
    Sabina Ramsey
    Participant

    This reply is private.

    #1329018
    Sabina Ramsey
    Participant

    I kept searching around online for anyone else who was trying to accomplish this and it seems that this code works, just wanted to share it for anyone else trying this.

    // Exclude past events from search
    function mySearchFilter($query) {
        if ($query->is_search) {
            $past_events = tribe_get_events( array(
              'posts_per_page' => -1,
              'end_date' => new DateTime()
            ) );
            $exclude_events = [];
            foreach ( $past_events as $past_event ) {
                $exclude_events[] = $past_event->ID;
            }
            $query->set('post__not_in', $exclude_events);
        }
        return $query;
    }
    add_filter('pre_get_posts','mySearchFilter');
    • This reply was modified 6 years, 9 months ago by Sabina Ramsey.
    #1329206
    Victor
    Keymaster

    That’s great Sabina! 🙂

    Thanks for sharing that code snippet so other users can benefit from it.

    As you marked this resolved I’ll go ahead and close it, but feel free to open a new thread if anything comes up.

    Best,
    Victor

Viewing 10 posts - 1 through 10 (of 10 total)
  • The topic ‘Exclude past events from WP search’ is closed to new replies.