Estimated reading time: 3 minutes
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:
Then on the right hand side are the files you can edit. You should click on “Theme Functions (functions.php)“:
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:
Once in the category section you will see all your categories listed on the right hand side:
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=:
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.
Share this content: