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.
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?
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.
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.