Exclude Events Category from all queries (but not for admins)

Home Forums Calendar Products Events Calendar PRO Exclude Events Category from all queries (but not for admins)

  • This topic has 2 replies, 3 voices, and was last updated 9 years ago by Diego Hinz.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1272009
    Diego Hinz
    Participant

    Dear Support Team,

    awhile ago I started a thread asking to exclude a certain category from all queries, but not for the administrator. I had found several topics but the snippets posted there did not seem to work for me: Whenever I used the list-view widget (TEC pro), all filters, even those set by the widget, were removed.

    I have now managed to create a function that filters a specific category on all queries:

    function tribe_exclude_events_category( $wp_query ) {
    // only filter the query if the user cannot edit pages (< editor)
    		 if (!current_user_can('edit_pages')) {
    			$exclude_cats = array('exclude-me'); 
    
    			// Join with current tax query if set
    			if (is_array($wp_query->get( 'tax_query')))
    				{$tax_query =$wp_query->get( 'tax_query');}
    			else
    				{$tax_query = array();}
    
    			// Setup an exclude from the tribe_events_cat taxonomy
    			$tax_query[] = array(
    				'taxonomy'  => Tribe__Events__Main::TAXONOMY,   // sidebar ist: "tribe_events_cat"
    				'field'     => 'slug',
    				'terms'     => $exclude_cats,
    				'operator'  => 'NOT IN'
    			);
    
    			$wp_query->set('tax_query', $tax_query);
    		}
    
    		return $wp_query;
    }
    
    add_action( 'pre_get_posts', 'tribe_exclude_events_category', 10, 1 );

    This is basically the code from this topic with a couple of changes. The most significant is, that the wrong tax_query was selected by the snippet in the other thread:
    It must be $wp_query->get( 'tax_query') instead of $wp_query->tax_query

    I just wanted to share this with the TEC community – maybe it will be helpful for others in the future.

    Happy coding, everyone!

    #1273133
    Trisha
    Member

    Hello, Diego!

    Thank you for using The Events Calendar and thank you so much for your kindness in sharing this with the community!

    Have a great weekend!

    Cheers,
    Trisha

Viewing 2 posts - 1 through 2 (of 2 total)
  • The topic ‘Exclude Events Category from all queries (but not for admins)’ is closed to new replies.