Our support team answers many questions every day, and recently, we’ve received some questions about how to automatically add a noindex tag for all occurrences of a recurring event.

In one case, a user mentioned that they prefer creating a page for the event and not having all recurring event occurrences indexed by Google.

The good news is that it is possible to achieve this with the following PHP snippet:

add_action( 'wp_head', 'tec_add_nofollow_to_recurring_events' );

/**
 * Add 'nofollow' to recurring events, except for the first instance.
 * 
 * @return void
 */
function tec_add_nofollow_to_recurring_events() {
    // Bail if ECP is not active.
    if ( ! class_exists( 'Tribe__Events__Pro__Main' ) ) {
		return;
    }

    // Get the post ID.
    $event_id = get_the_ID();

    // Bail, if it's not an event.
    if ( get_post_type( $event_id ) != "tribe_events" ) {
		return;
    }
	
    // Bail, if it's not a single event
    if ( ! is_single() ){
        return;
    }
	// If it's a recurring event, add the noindex meta tag
    if ( tribe_is_recurring_event($event_id) ) {
		echo '<meta name="robots" id="tec_noindex" content="noindex, follow" />' . "\n";
    }
}

Although that works for the above scenario, we do see other scenarios around this request as well.

If you’re looking to have only the first recurrence indexed by Google but not all the subsequent ones, you’ll need to tweak that code not to include the noindex tag for the first event occurrence.

add_action( 'wp_head', 'tec_add_nofollow_to_recurring_events' );

/**
 * Add 'nofollow' to recurring events, except for the first instance.
 * 
 * @return void
 */
function tec_add_nofollow_to_recurring_events() {
    // Bail if ECP is not active.
    if ( ! class_exists( 'Tribe__Events__Pro__Main' ) ) {
		return;
    }

    // Get the post ID.
    $event_id = get_the_ID();

    // Bail, if it's not an event.
    if ( get_post_type( $event_id ) != "tribe_events" ) {
		return;
    }
	
    // Bail, if it's not a single event
    if ( ! is_single() ){
        return;
    }
	
    if ( tribe_is_recurring_event($event_id) ){
        // Get the post object.
	    $post = get_post( $event_id );
	
	    // Check if the event is the first in the recurrence.
	    $is_first = \TEC\Events\Custom_Tables\V1\Models\Occurrence::is_first( $post->_tec_occurrence );

	    // If it's not the first recurrence, add the noindex meta tag
	    if ( !$is_first ) {
		    echo '<meta name="robots" id="tec_noindex" content="noindex, follow" />' . "\n";
	    }
    }
}