This code works perfectly
function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-1' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );
But this code does not work at all
$caid = "-1";
function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', $caid );
}
}
add_action( 'pre_get_posts', 'exclude_category' );
$caid
is unknown inside the function, unless declared global.// Edit
Do you need the variable outside the function at all? If not, just move it inside the function, then you can ommit the
global
.PHP: Variable scope
You can as well use closures in PHP:
This is a function returning a function. Therefore you can pass in the catid as a parameter at the time when you register the
pre_get_posts
action.