How to Exclude Specific Categories from the Homepage?

If you want to hide certain categories from the list of latest posts on your homepage, you can do it easily with a small code snippet. First, install and activate the ProFunctions plugin, then add this code:

function custom_exclude_categories( $query ) {
    if ( $query->is_home && $query->is_main_query() ) {
        // Replace the IDs with the categories you want to exclude, for example 1, 5, 10
        $query->set( 'category__not_in', [ 1, 5, 10 ] );
    }
}
add_action( 'pre_get_posts', 'custom_exclude_categories' );

Need help finding a category ID? Check out the instructions here.

Did the answer help you?
Related Questions