Only show next event in search results

Home Forums Calendar Products Events Calendar PRO Only show next event in search results

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1125922
    David
    Participant

    HI 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?

    #1126208
    Josh
    Participant

    Hey 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!

    #1130752
    David
    Participant

    Couple 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
    #1131337
    David
    Participant

    Okay, 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?

    #1132259
    Josh
    Participant

    Hey 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!

    #1132695
    David
    Participant

    Thanks 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.

    #1132700
    David
    Participant

    Well, after all my mucking about, the code posted in this thread appears to work.

    #1133223
    Josh
    Participant

    Hey 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!

Viewing 8 posts - 1 through 8 (of 8 total)
  • The topic ‘Only show next event in search results’ is closed to new replies.