Posted on

Showing only one recurring event

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, added to your theme functions.php, 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 post publication date or something else.

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 (!$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]) { 
                $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;
}

Leave a Reply

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