Hiding a category of events from the month view in WordPress Events Calendar
We frequently use the WordPress Events Calendar plugin to handle events on our clients’ websites. Occasionally the client requests a feature that isn’t built in to the calendar, which means we must find a way to solve the problem.
Recently, one of our school clients had the following problem. They wanted all events in Category A to show up on the main calendar, and for events in Category B to be hidden from the main calendar. However, if an event was categorized as both A and B, it needed to be shown on the main calendar.
This is not a built-in feature of the Events Calendar, and so required adding a custom function, which you can see below:
/* Remove Events from Month and List Views */ add_action( 'pre_get_posts', 'tribe_exclude_events_category_month_list' ); function tribe_exclude_events_category_month_list( $query ) { if ( isset( $query->query_vars['eventDisplay'] ) && ! is_singular( 'tribe_events' ) ) { if ( $query->query_vars['eventDisplay'] == 'list' && ! is_tax( Tribe__Events__Main::TAXONOMY ) || $query->query_vars['eventDisplay'] == 'month' && $query->query_vars['post_type'] == Tribe__Events__Main::POSTTYPE && ! is_tax( Tribe__Events__Main::TAXONOMY ) && empty( $query->query_vars['suppress_filters'] ) ) { $query->set( 'tax_query', array( 'relation' => 'OR', array( 'taxonomy' => Tribe__Events__Main::TAXONOMY, 'field' => 'slug', 'terms' => array( 'nursery' ), // CATEGORY B SLUG 'operator' => 'NOT IN' ), array( 'taxonomy' => Tribe__Events__Main::TAXONOMY, 'field' => 'slug', 'terms' => array( 'whole-school' ), // CATEGORY A SLUG 'operator' => 'IN' ) ) ); } } return $query; }
If you wish to use this solution ON YOUR OWN WORDPRESS SITE, simply copy the code above and enter your own category slugs (ensuring you add the category slug of the category you wish to hide in place of ‘category-B’).
The outcome for this school client was a main calendar that displayed all events except those marked as ‘Nursery’, however if the events were categorized as both ‘Nursery’ and ‘Whole School’, they would show up in the main calendar. The category B events (‘Nursery’ in this case) will only be hidden from the main calendar and will still show up on the event category archive page, which you can find at: yoursite.co.uk/events/category/category-slug/.
Check back soon for more TWK developer tips!