WordPress – How to Exclude a Category from your Homepage

How to Exclude a Category from your Homepage

Introduction

If you are a WordPress user, you may want to find a way of excluding a certain category from your main homepage. This could be if you post latest news articles but do not want them to clutter up your homepage.

The reason I wanted to exclude a certain category was that I run daily giveaways through a partner site and did not want them on the homepage, as it would just be full of them and none of my other posts!

There are a few methods to carry this out, but I am going to show you how to do this with some simple code. However, if you would rather use a WordPress plugin, then you should check out this part of the article:

Use a WordPress Plugin

WordPress plugins are great and usually very simple to use. However, you have to take into consideration things like:

“Will it slow your website down?”

“Is the plugin secure?”

Will the plugin be compatible with my website?

Will the plugin break my website?

But, let’s be honest, most plugins are perfectly safe so long as they are kept up to date and you should always check out the reviews for them to help you make your decision.

That being said, before I decided to go with the coded method, I did try out these three WordPress plugins and they all did the job perfectly well. However, due to the amount of plugins I use already, I decided that I did not want another one for this process.

Use Code

Some people bulk at the mere thought of adding additional code into the WordPress core files. However, it really is not that complicated to make minor changes to get excellent results.

Category ID

Before you start with any coding, you will first need the category ID of the category you want to exclude from your homepage. This is a unique number allocated to each category within your own WordPress installation.

To get this, go to your WordPress dashboard and go to Posts > Categories:

WordPress Portal Categories Menu
WordPress Portal Categories Menu

You will now see a list of all the categories that you have in your WordPress. Find the category that you want to exclude and then click on the Edit link:

WordPress Edit Category
WordPress Edit Category

This will now take you to the detailed area of the category where you can make a number of changes. However, we are interested in the browser address bar. For this guide I am using Google Chrome, but it should be the same with other browsers like Firefox or Edge.

Looking in the address bar, you will see in the middle of the URL that it has category&tag_ID=530 (530 being my category that I am excluding – yours will be different!):

WordPress Category Tag ID
WordPress Category Tag ID

The number is what you need – this is the ID of the category. In my example above, the category ID that I want to exclude from my homepage is 530.

Backup your WordPress

Now before we go any further, any mistakes here could break your website. So ensure that you follow the instructions carefully, but most importantly, backup your site now! Check out the most reliable WordPress backup service for your business to carry out a backup.

At least then, if the worst was to happen, you can revert your site back.

Theme Editor

Now that you have the category ID of the category you want to exclude from your homepage, you will need to add some code to the functions.php file.

To do this, hover over the Appearance link and then click on the Theme Editor link:

WordPress Theme Editor Menu
WordPress Theme Editor Menu

This will take you to the Theme Editor where you can now edit your themes code. The first thing you will need to do is to look towards the top right of the screen and select from the drop down, the theme that you want to edit. When you have selected your theme from the drop down, click on the Select button:

WordPress Select Theme Drop Down
WordPress Select Theme Drop Down

Once you have done that, you will see a list of files underneath this drop down. Find and click on the file named functions.php:

WordPress Theme Functions File
WordPress Theme Functions File

The code

Now to the left of this area, you will see the code that forms the functions.php file. You should now copy and paste the below code into this functions.php file:

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

Note the red -530 part of the above code – this means minus category 530. So yes you guessed it, you need to delete the 530 and replace with your categories ID that you retrieved earlier in this guide.

Once you are happy that you have the correct code in place, you should now double and triple check that there are no syntax errors. Sometimes when copying and pasting code, some special characters can become malformed – especially quote marks and dollar signs like are included in this code. The best thing to ensure everything is correct is to manually delete and type in these special characters.

Now you are really happy everything is correct, go ahead and click on the Update File button at the bottom left of the screen.

Refresh your homepage (clear your caches if you have them in place) and you should now see that the category is no long on your homepage!

Feedback

We would love to hear your feedback on this one so please feel free to leave us a message below in our comments section.

About A.J. Armstrong

Founder of TechyGeeksHome and Head Editor for over eight years! IT expert in multiple areas for over 21 years. Sharing experience and knowledge whenever possible! Making IT Happen.

View all posts by A.J. Armstrong

Leave us a message...

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