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' );