Home › Forums › Calendar Products › Events Calendar PRO › Exclude past events from WP search
- This topic has 10 replies, 2 voices, and was last updated 6 years, 9 months ago by Sabina Ramsey.
-
AuthorPosts
-
July 14, 2017 at 10:45 am #1321389Sabina RamseyParticipant
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?
July 17, 2017 at 1:41 pm #1322311VictorKeymasterHello 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!
VictorJuly 17, 2017 at 2:32 pm #1322339Sabina RamseyParticipantI 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
July 18, 2017 at 7:56 am #1322661Sabina RamseyParticipantActually 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.
July 18, 2017 at 1:58 pm #1322901VictorKeymasterHello 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,
VictorJuly 19, 2017 at 11:01 am #1323466Sabina RamseyParticipantI’ve added that code, tried flushing out the cache and checked again and I am still seeing past events.
July 19, 2017 at 7:47 pm #1323649VictorKeymasterHello 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,
VictorAugust 1, 2017 at 12:14 pm #1328993Sabina RamseyParticipantThis reply is private.
August 1, 2017 at 12:42 pm #1329018Sabina RamseyParticipantI 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.
August 1, 2017 at 7:19 pm #1329206VictorKeymasterThat’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 -
AuthorPosts
- The topic ‘Exclude past events from WP search’ is closed to new replies.