Introducing how to exclude posts from specific categories from the post list using WordPress API hooks and PHP. Use the exclude_category() function as follows:
function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-3' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );
Explanation:
‘cat’ doesn’t mean excluding the cat category… this is more like the string itself meaning category. So don’t change this.
The second argument -3. This is the category.
Categories are assigned numeric values, for example, http://example.com/blog/?cat=1 is a category page. This is a category named “Blog”, and this category’s numeric value is 1.
If you want to exclude the “Blog” category from the post list, write -1.
To specify multiple categories, separate them with ,.
Reference: