How to exclude category from WordPress blog page

If you want to exclude category from the WordPress blog page or home page then you do not need to install a plugin. You need to follow step by step our “exclude category” WordPress tutorial. By just modifying a file get the exclude category result you want in minutes. In this tutorial we include the sample code also for beginners.

Tutorial to exclude category from WordPress blog page

1. Login to your WordPress dashboard and find the WordPress category ID of the category you need to exclude. Go to Posts > Categories:

Find Category

2. Mouse over the category name (that you want to exclude from blog page) to see its ID.

Find Category ID

3. Download the functions.php file from your theme (e.g. /wp-content/themes/themename/functions.php). Before doing any changes to the functions.php file, make a backup of your theme’s file in case you need to quickly get back the changes.

4. Copy the below code and paste it in functions.php file (need to carefully place your code in functions.php file). If you want to add a comment to identify the code at a later stage, you can do so by using the sign ‘//’ (for single line comment) and ‘/* …. */’ (multiple lines comment).

PHP code to exclude WordPress category from blog

function exclude_category($query) {
if ( $query->is_home() ) {
$query->set('cat', '-xx');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');

5. Replace the xx with the category ID you see in step 2 of this WordPress tutorial. Please leave the minus ‘-‘ sign in front of the category ID, only replace the xx with Category ID.

6. After save the file and upload it back to your website. Refresh the blog page and you should notice that the excluded category is not showing up in the WordPress blog.

Excluding multiple WordPress categories from blog page

To exclude multiple categories from appearing in the WordPress blog page, just add all of the categories ID in the same line separate them with comma in the code as per the example below.

$query->set(‘cat’, ‘-12, -15, -32’);

The above code would exclude the categories with ID 12, 15 and 32.

2 thoughts on “How to exclude category from WordPress blog page”

  1. This is what I was looking for. I wanted to exclude 1 category from the search results. There is no such feature inside wordpress itself. Now I know how to deal with it.

    Reply

Leave a Comment