Skip to main contentSkip to footer

Some event calendar plugins do recurring events by creating many posts. That’s fine until those cloned posts fill up your search results. This function will only show one of each post with the same title and will take the one with the first date.

The date is checked from _EventStartDate custom field, but it’s easy to change the function to look at the post publication date or something else.

Add this to your site:

add_filter( 'relevanssi_hits_filter', 'rlv_cull_recurring_events' ); 
function rlv_cull_recurring_events( $hits ) { 
  $ok_results     = array(); 
  $posts_seen     = array(); 
  $index_by_title = array(); 
  $date_by_title  = array(); 
  $i = 0; 
  foreach ( $hits[0] as $hit ) { 
    if ( ! isset( $posts_seen[ $hit->post_title ] ) ) { 
      $ok_results[]                       = $hit; 
      $date_by_title[ $hit->post_title ]  = get_post_meta( $hit->ID, '_EventStartDate', true ); 
      $index_by_title[ $hit->post_title ] = $i; 
      $posts_seen[ $hit->post_title ]     = true; 
      $i++; 
   } else { 
      if ( get_post_meta( $hit->ID, '_EventStartDate', true ) < $date_by_title[ $hit->post_title ] ) { 
        if ( strtotime( get_post_meta( $hit->ID, '_EventStartDate', true ) ) < time() ) {
          continue; // Skips events that are in the past.
        }
        $date_by_title[ $hit->post_title ]                 = get_post_meta( $hit->ID, '_EventStartDate', true ); 
        $ok_results[ $index_by_title[ $hit->post_title ] ] = $hit;
      } 
    } 
  } 
  $hits[0] = $ok_results;
  return $hits;
}

Update 27.10.2017: Fixed the function to remove an “Undefined index” error and added a check for skipping events in the past.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

WooCommerce: Aelia Prices by Country product visibility

…$post = get_post( $post_id ); $post_type = $post->post_type; $product_id = $post->ID; if ( ‘product‘ === $post_type && function_exists( ‘wc_get_product’ ) ) { // Get the product object. $product = wc_get_product( $product_id ); // Check if the product is purchasable. if ( $product && ! $product->is_purchasable() ) { $post_ok = false;……Check if the product is purchasable. if ( $product && ! $product->is_purchasable() ) { $post_ok = false; } } return $post_ok; } This function uses the relevanssi_post_ok filter hook to control which posts are included in the search and the $product->is_purchasable() function to see if the product can be shown….Aelia has a Prices by Country for WooCommerce plugin, which can adjust prices based on customer country and hide products unavailable in a specific country. Jason James shared a function that makes Relevanssi hide unavailable products in search: add_filter( ‘relevanssi_post_ok’, ‘relevanssi_aelia_compatibility’, 10, 2 ); function relevanssi_aelia_compatibility( $post_ok, $post_id ) {…

Related Posts:

Comment Section:

2 Comments. Leave new

  • I’m using the Modern Tribe events plugin on several of my sites. Is there a way to alter this function to just exclude all past events from search altogether? It’s starting to become a real headache.

    Reply
    • Yes, it’s possible. I don’t know how Modern Tribe stores the dates, but it shouldn’t be difficult to create a filter to exclude all past events. It would be simpler than this: inside the foreach loop, just check the date, and if it’s in the past, skip the post.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed