cliff
9/25/2017 - 1:03 PM

The Events Calendar: If user is logged out, do not display events in specified Event Categories.

The Events Calendar: If user is logged out, do not display events in specified Event Categories.

<?php

/**
 * The Events Calendar: If user is logged out, do not display events in
 * specified Event Categories.
 *
 * !!! Make sure to alter $excluded_event_cats !!!
 *
 * @link https://gist.github.com/cliffordp/799c1b546b79ad7626141bdc00974f53
 */
add_action( 'tribe_events_pre_get_posts', 'cliff_exclude_posts_in_taxonomy_if_logged_out' );
function cliff_exclude_posts_in_taxonomy_if_logged_out( $query ) {
	// don't have to return $query, since it's passed by reference (i.e. changes to $query are real-time)

	// Only modify the main query
	if ( ! $query->is_main_query() && ! is_admin() ) {
		return;
	}

	if ( is_user_logged_in() ) {
		return;
	}

	// MODIFY THIS !!!
	$excluded_event_cats = array(
		'logged-in-only',
		'bbq',
		'heritage',
	);

	$query->set( 'tax_query', array(
			array(
				'taxonomy' => Tribe__Events__Main::TAXONOMY,
				'field'    => 'slug',
				'terms'    => $excluded_event_cats,
				'operator' => 'NOT IN'
			)
		)
	);
}