Help Desk

Exclude Category from BOTH Gridview and Listview

  • Posts: 12 Topics: 5 Answers: 2
    | Permalink

    Hi. I need some help. Can’t figure it out how to exclude categories both Gridview and Listview. Looked through this forum, but there is no good solution.

    1. U can exclude it from Grid (functions.php) –
    2. Or u can exclude it from Grid (table.php) –
    3. Also u can exclude it from List (list.php) –

    1 and 2 works fine.
    3 works, BUT it crashes pages and page selection links in listview (same over here – https://theeventscalendar.com/support/forums/topic/removing-one-category-of-events-from-main-event-list-and-calendar/ )

    So I kinda need a solution that excludes it from both type of view.

    Posts: 12 Topics: 5 Answers: 2
    | Permalink

    Eh, I need some support over here…

    Posts: 4021 Topics: 4 Answers: 763
    | Permalink
    Correct Answer

    Hey Denis, how about this:

    add_action( 'pre_get_posts', 'exclude_events_category' );
    function exclude_events_category( $query ) {
    
    if ( $query->query_vars['eventDisplay'] == 'upcoming' || $query->query_vars['eventDisplay'] == 'past' || $query->query_vars['eventDisplay'] == 'month' && $query->query_vars['post_type'] == TribeEvents::POSTTYPE && !is_tax(TribeEvents::TAXONOMY) && empty( $query->query_vars['suppress_filters'] ) ) {
    	$query->set( 'tax_query', array(
    		array(
    			'taxonomy' => TribeEvents::TAXONOMY,
    			'field' => 'slug',
    			'terms' => array('presentations'),
    			'operator' => 'IN'
    		)
    		)
    	);
    }
    
    return $query;
    
    }
    
    Posts: 12 Topics: 5 Answers: 2
    | Permalink

    You mean including only those categories that i need? It’s not a solution for me – client won’t be happy to ask me changing php-file every time he adds a new category.

    Posts: 4021 Topics: 4 Answers: 763
    | Permalink

    Hey Denis,

    No you change the operator used for the tax_query to ‘NOT IN’ and then have it exclude. You can read more up on tax_query here: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

    I hope that helps!

    – Jonah

    Posts: 12 Topics: 5 Answers: 2
    | Permalink

    Thank you! It did help!

    Posts: 12 Topics: 5 Answers: 2
    | Permalink

    But there is 1 more thing…
    Now i have a hidden category for second calendar.
    This category is hidden from both gridview and listview – http://itrackdays.ru/events/
    And u can see it in category’s gridview – – http://itrackdays.ru/events/category/karting-2
    But category’s listview is empty – http://itrackdays.ru/events/category/karting-2/predstoyashhee

    Posts: 4021 Topics: 4 Answers: 763
    | Permalink

    Hi Denis,

    Last little tip I can provide on this, you’ll need to figure out the rest on your own or hire a developer. Try adding these additional conditions to only filter out the category to be excluded if we are not on a category for the upcoming or past lists:

    add_action( 'pre_get_posts', 'exclude_events_category' );
    function exclude_events_category( $query ) {
    
    if ( $query->query_vars['eventDisplay'] == 'upcoming' && !is_tax(TribeEvents::TAXONOMY) || $query->query_vars['eventDisplay'] == 'past' && !is_tax(TribeEvents::TAXONOMY) || $query->query_vars['eventDisplay'] == 'month' && $query->query_vars['post_type'] == TribeEvents::POSTTYPE && !is_tax(TribeEvents::TAXONOMY) && empty( $query->query_vars['suppress_filters'] ) ) {
    	$query->set( 'tax_query', array(
    		array(
    			'taxonomy' => TribeEvents::TAXONOMY,
    			'field' => 'slug',
    			'terms' => array('2012'),
    			'operator' => 'NOT IN'
    		)
    		)
    	);
    }
    
    return $query;
    
    }
    

    I hope that helps!

    – Jonah

    Posts: 65
    | Permalink

    This topic has not been active for quite some time and will now be closed.

    If you still need assistance please simply open a new topic (linking to this one if necessary)
    and one of the team will be only too happy to help.