Don’t display WordPress posts from certain category?

So you want to hide some posts from the front page, posts from a certain category.

First thing first, create your new category.

Then open functions.php. Your theme might have a file called functions.php. If it doesn’t, you need to open the raw functions.php file from wordpress.

Add this at the end of functions.php (from your theme).

1
2
3
4
5
6
function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-13' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

What you need to modify is “-13”, replace the number with the number coresponding to the category you want to hide from main page.
You can hover with the mouse on the category from wp-admin / category creation page to find out the number.
Keep the minus, just replace 13.

That is all, upload functions.php and refresh the page.

If you want to remove more categories, replace the $query like this: $query->set( ‘cat’, ‘-13, -14, -2’ );

ADD A COMMENT: