Home › Forums › Calendar Products › Events Calendar PRO › Exclude categories from "Related Events"
- This topic has 4 replies, 3 voices, and was last updated 7 years, 3 months ago by Marlene Hassel.
-
AuthorPosts
-
January 25, 2017 at 4:39 am #1223504Marlene HasselParticipant
Is there a way to do that?
Some events have multiple categories, and I need some of them to be overlooked by the events calendar when deciding which events are related.January 25, 2017 at 4:06 pm #1224075GeorgeParticipantHey @Marlene,
There’s unfortunately no way to exclude specific categories or tags from the “Related Events” queries without writing custom code. đ
We are not able to help with custom coding as elaborated upon here. However, to help point you in the right direction for achieving this, you can find the function that gets “Related Events” in this file within your Events Calendar Pro plugin files:
events-calendar-pro/src/functions/template-tags/general.php
âď¸ The function here is called tribe_get_related_postsâyou can find it around line 695 and read into how it functions.
A filter hook exists there called tribe_related_posts_args that you can use from inside something like your theme’s functions.php file to exclude categories or tags from the Related Events queries. You would probably have the most success using something like tax_query, which you can read about here â https://codex.wordpress.org/Class_Reference/WP_Query
Here’s an example that may work wellâwhat it does is exclude the event category “barbecue”:
if ( class_exists( 'Tribe__Events__Main' ) ) {add_filter( 'tribe_related_posts_args', 'example_exclude_category_from_related_posts' );
function example_exclude_category_from_related_posts( $args ) {
if ( ! is_array( $args ) ) {
return $args;
}$args['tax_query'][] = array(
'taxonomy' => Tribe__Events__Main::TAXONOMY,
'field' => 'slug',
'terms' => array( 'barbecue' ),
'operator' => 'NOT IN',
);return $args;
}
}
âď¸ This example may help you get startedâyou can use ‘post_tag’ instead of Tribe__Events__Main::TAXONOMY if you want to target tags instead of event categories, and can tinker using all of the other information I’ve shared to try and guide your tinkering.
I hope this helps!
Cheers,
GeorgeJanuary 26, 2017 at 7:08 am #1224336Marlene HasselParticipantThanks!
I got it working now.
Here’s what I ended up with:add_filter( 'tribe_related_posts_args', 'example_exclude_category_from_related_posts' ); function example_exclude_category_from_related_posts( $args ) { if ( ! is_array( $args ) ) { return $args; } //Remove special categories $ej_relaterade=array('15', '31', '35', '37', '39', '41', '42', '43', '44', '47'); $args["tax_query"][0]["terms"] = array_diff($args["tax_query"][0]["terms"],$ej_relaterade); return $args; } //}
I had to remove the outer
if ( class_exists( 'Tribe__Events__Main' ) )
to get the function to do anything. What was that if-statement for?January 26, 2017 at 9:17 am #1224499GeorgeParticipantThat if statement is optional and just helps to prevent this code from running if The Events Calendar isn’t present on your site.
But no worriesâglad you’ve got things working. Thanks for sharing your version of thisâbe sure to keep backups of this custom code so that you don’t lose it when you update your themes and plugins down the road!
Cheers,
George -
AuthorPosts
- The topic ‘Exclude categories from "Related Events"’ is closed to new replies.