Exclude Certain Categories from the Homepage on WordPress Blogs

wordpress blue and white and grey logo

Last updated on April 17th, 2023 at 12:22 am

Read Time:2 Minute, 15 Second

If you are using WordPress for your blog then you will probably be using your latest posts to make up your home page.

By default, all categories of posts are included in your home page, but what if you want to exclude a category or multiple categories of posts from displaying on your home page?

Well, it is pretty simple…

First you need to edit your themes functions.php file. To do this, login to your WordPress Dashboard and click Appearance > Editor:

Exclude Certain Categories from the Homepage on WordPress Blogs 1

Then on the right hand side are the files you can edit. You should click on “Theme Functions (functions.php)“:

Exclude Certain Categories from the Homepage on WordPress Blogs 2

You should then add this code:

function excludeCat($query) {
if ( $query->is_home ) {
$query->set(‘cat’, ‘-6,-9,-17’);
}
return $query;
}
add_filter(‘pre_get_posts’, ‘excludeCat’);

Here you will see -6,-9,-17, these are the important numbers that you need to amend. These are actually the category ID’s of the categories that you want to exclude from showing on your home page.

So how do you find your category ID so that you can amend them in the above code?

All you have to do is click on Posts > Categories:

Exclude Certain Categories from the Homepage on WordPress Blogs 3

Once in the category section you will see all your categories listed on the right hand side:

Exclude Certain Categories from the Homepage on WordPress Blogs 4

If you right click on the category you want to exclude and open in a new tab, you should now see in the address bar of your browser tag&categoryID=:

Exclude Certain Categories from the Homepage on WordPress Blogs 5

Next to this will be a number – this is the number of this category. In our example, the number is 9.

So, you would then enter the code with a -9 to exclude that category. So as per our example, the code would then be:

function excludeCat($query) {
if ( $query->is_home ) {
$query->set(‘cat’, ‘-9’);
}
return $query;
}
add_filter(‘pre_get_posts’, ‘excludeCat’);

One you are happy that you have the correct code in place you can go ahead and save the functions.php file in your theme.

Now, when you load your home page, the category you added to the functions.php file will now not show on your home page.

Note

This is a theme edit so any updates will over write it. So, you either will need to re-add it after any updates or what you really should do is your a child theme so you only have to carry this out once without fear of a theme update wiping out all your hard work!

Comments

If you have any questions or feedback on this guide, please feel free to leave us a message below and we will try and reply as soon as we can.

Click to rate this post!
[Total: 0 Average: 0]

Free Subscription

If you want to be notified when we post more quality guides like this one, sign up to our free subscription service and you will receive an email when a new post is live.

Join 441 other subscribers.

No need to worry, we will not be filling your inbox with spam and you can unsubscribe anytime you like.


Leave us a message...

This site uses Akismet to reduce spam. Learn how your comment data is processed.