exclude category from wordpress if not required

I’m having a bad time trying to make wordpress querys excluding categories unless asked.

I mean, if not specifically asked (ex: $args= array(‘cat’=>’-35′)) , the loop excludes this category.

Read More

what I got so far:

function exclude_categories( $wp_query ) {
    $excluded_cats = array( '-35' , '-36' );
    set_query_var( 'category__not_in', $excluded );
}
add_action( 'pre_get_posts', 'exclude_categories' );

This is doing ok excluding posts with this category but then if I try to ask for posts with this category it wont display any.

Any sugestions?

Related posts

1 comment

  1. Can you show the whole code with when you ask for posts including this category?

    Basically, you can set these parameters to inlucde or exclude categories in the query.

        'category__not_in'=> array(1,2),
        'category__in' => array(3,3)
    

    However in your example you have already pre-set set_query_var( 'category__not_in', $excluded );
    categories so even if you use category__in the query won’t use it because of the usage of category__not_in. You need to set this parameter to empty value.

Comments are closed.