Exclude categories from "Related Events"

Home Forums Calendar Products Events Calendar PRO Exclude categories from "Related Events"

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1223504
    Marlene Hassel
    Participant

    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.

    #1224075
    George
    Participant

    Hey @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,
    George

    #1224336
    Marlene Hassel
    Participant

    Thanks!
    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?

    #1224499
    George
    Participant

    That 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

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Exclude categories from "Related Events"’ is closed to new replies.