Home › Forums › Calendar Products › Events Calendar PRO › Only show next event in search results
- This topic has 8 replies, 3 voices, and was last updated 9 years, 11 months ago by
Josh.
-
AuthorPosts
-
June 12, 2016 at 4:56 pm #1125922
David
ParticipantHI all,
Am trying to customize my search results to only show the next upcoming event in a series of recurring events, rather than every event. This saves having 20+ pages of search results with the same event.
I’ve managed to find a filter to only search top level posts as I read that the calendar creates recurring events as children of the first post.
Here’s what I’ve got so far:
add_action( 'pre_get_posts', 'hide_recurring_event_search' ); function hide_recurring_event_search( $query ) { if ( !is_admin() && $query->is_main_query() ) { if ($query->is_search) { $query->set('post_parent', '0' ); } } }This hides all the child events as expected, but the only event that shows up is the first in the series, and is therefore irrelevant because it’s in the past.
Any tips or assistance on how I could finish this off to get it working as expected?
June 13, 2016 at 10:22 am #1126208Josh
ParticipantHey David,
Thanks for reaching out to us!
Looks like you’re on the right path here! The parent of the recurring event will always be the very first event that was created. To get the next upcoming event, you can take the same approach but with a meta_query within your “pre_get_posts()” function there.
Try something like:
$query->set( 'meta_key', '_EventStartDate' ); $query->set( 'meta_value', date('Y-m-d') . ' 00:00:00' ); $query->set( 'meta_compare', '>=' ); $query->set( 'posts_per_page', 1 );Let me know if this helps.
Thanks!
June 22, 2016 at 7:47 pm #1130752David
ParticipantCouple of minor issues with the code- appears to be getting close though.
First: I only get one search result per page.
Second: Searches list both the parent and next child events.Site this is on is now semi-public, so I can point you at http://linkup.org.nz/?s=food and you’ll see what I mean.
add_action( 'pre_get_posts', 'hide_recurring_event_search' ); function hide_recurring_event_search( $query ) { if ( !is_admin() && $query->is_main_query() ) { if ($query->is_search) { $query->set( 'post_parent', '0' ); $query->set( 'meta_key', '_EventStartDate' ); $query->set( 'meta_value', date('Y-m-d') . ' 00:00:00' ); $query->set( 'meta_compare', '>=' ); $query->set( 'posts_per_page', 1 ); } } }-
This reply was modified 9 years, 11 months ago by
David. Reason: More detail
June 23, 2016 at 9:04 pm #1131337David
ParticipantOkay, after some more investigation i’ve updated my code:
add_filter( 'pre_get_posts', 'hide_recurring_event_search' ); function hide_recurring_event_search( $query ) { if ( ! is_admin() && $query->is_main_query() ) { if ( $query->is_search ) { $query->set( 'meta_key', '_EventEndDate' ); $query->set( 'meta_value', date('m-d-Y') . ' 00:00:00' ); $query->set( 'meta_compare', '>=' ); $query->set( 'tribeHideRecurrence', true ); $query->tribe_is_multi_posttype = true; } } }However, my search results are now purely events, rather than including posts/pages/events and other custom post types (which I need to do.)
Any thoughts on how I could fix the code so searches include everything?
June 27, 2016 at 7:18 am #1132259Josh
ParticipantHey David,
Thanks for following up with us here.
I’ve been thinking about this one and on second thought here, I believe the best approach to get everything that you’re describing here would be to modify the output of the results rather than filtering what’s retrieved.
With this, you could have a series of conditionals for the search results loop that checks to see if the result is an event, is a recurring event, and if the date is after the current date. If it is, you could show the first result that matches all of those conditionals and then skip the rest.
Let me know if this helps.
Thanks!
June 27, 2016 at 5:02 pm #1132695David
ParticipantThanks for your follow-up Josh.
I’ve modified my search loop to the below code. Note that I’ve removed the templating for brevity’s sake, but it does successfully identify recurring events.
<?php if ( get_post_type() == 'tribe_events' && tribe_is_recurring_event() == 1 ) : ?> <!--Recurring Event--> <?php else : ?> <!--Everything Except Recurring Events--> <?php endif; ?>Any tips on how I might make the final tweak to only pull the next recurring event?
Cheers.
June 27, 2016 at 6:07 pm #1132700David
ParticipantWell, after all my mucking about, the code posted in this thread appears to work.
June 28, 2016 at 4:59 pm #1133223Josh
ParticipantHey David,
I’m glad you were able to find a solution here!
I’ll go ahead and close this thread for now. If you have any further questions, please don’t hesitate to open a new one.
Thanks!
-
This reply was modified 9 years, 11 months ago by
-
AuthorPosts
- The topic ‘Only show next event in search results’ is closed to new replies.
