How to not display a category post on blog page?

Question:

As you know, we can assign a “Category” to any post we write. We can also use a “Category” as link on the menu. Good!

By default, WordPress will display all your posts in all the categories on your front page.

However, how to prevent WordPress displaying a “Category” type on the front page? Example, I have a post with a category name “Football”. I don’t want all posts with this category to be displayed on the front page, how to do that? I will access it only on the menu.

Thank

Answer:

You can use pre_get_posts

Exclude categories on your main page

function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-1,-1347' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

Leave a Comment

Your email address will not be published. Required fields are marked *