There’s an action you could hook into. It’s called pre_get_posts
and it lets you alter the query before the query is run.
This code should be placed into your functions.php file.
function my_exclude_category( $query )
{
if ( $query->is_singular('post') && $query->is_main_query() ) {
$query->set( 'cat', '-10' );
}
}
add_action( 'pre_get_posts', 'my_exclude_category' );
Please note. Your approach is not working because you are trying to create a new query within a theme template file that is supposed to be used to display a singular post. Also, you aren’t affecting the main query by creating a new WP_Query. That’s what the pre_get_posts
action hook is for. The template file that is used for displaying a list of posts is e.g. archive.php
please see the template hierarchy documentation for more information.