Your WordPress homepage should showcase your best content. But, what if you have categories that don’t belong there? In that case, you may want to exclude a specific category from your website’s homepage.
By default, WordPress displays posts from all categories on your homepage. This can lead to a cluttered experience, especially if you have categories meant for internal use or specific audiences.
In this article, we’ll show you how to easily exclude a category from your WordPress homepage using two of the best methods we’ve found.
Why Exclude a Category from WordPress Homepage?
WordPress allows you to sort content into categories and tags. Now, sometimes you may want to use a category for posts that are not part of your normal blog entries.
By default, WordPress doesn’t allow you to hide posts in specific categories from homepage or archives. This leaves you with unwanted posts appearing on your homepage.
That being said, let’s see how you can easily exclude a specific category from your WordPress homepage. We’ll show you two different methods, and you can choose the one that best fits your need.
Method 1: Exclude a Category from WordPress Using Plugin
The first thing you need to do is to install and activate the Ultimate Category Excluder plugin. For more details, you should follow our guide on how to install a WordPress plugin.
Upon activation, you’ll need to go to Settings » Category Excluder page. It will display all the categories that are available on your WordPress blog.
From here, you can simply check one or multiple categories under the ‘Exclude from front page’ column. If you want, then you can also exclude the category from RSS feeds, archives, or even from WordPress search.
Don’t forget to click on the ‘Update’ button to store these settings.
Now you can visit your website to see that the posts from the selected categories are excluded from the front page.
Method 2: Exclude a Category from WordPress Homepage Using Code
This method requires you to add code to your WordPress files. If you haven’t done this before, then see our guide on how to copy and paste code snippets in WordPress.
You will need to add the following code to your theme’s functions.php file, a site-specific plugin, or a code snippets plugin.
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-5' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
Don’t forget to replace ID (-5) with your category ID. It will hide all blog posts from homepage belonging to the category that matches this ID.
Note: Make sure to add a minus (-) sign with the category ID.
Not sure how to find the category ID? See our guide on how to find category IDs in WordPress.
The safest and easiest way to add this code in WordPress is by using the WPCode plugin. It allows you to add custom code without directly editing your WordPress files. So, you don’t have to worry about breaking your site.
To get started, you’ll need to install and activate the free WPCode plugin. If you need help, see this guide on how to install a WordPress plugin.
Once WPCode is activated, head to Code Snippets » + Add Snippet in your WordPress dashboard.
Then, hover your mouse over the ‘Add Your Custom Code (New Snippet)’ option and click the ‘Use snippet’ button underneath it.
From there, you need to select ‘PHP Snippet’ as the code type from the list of options that appear on the screen.
On the ‘Create Custom Snippet’ page, you can start by adding a title for your snippet. This can be anything to help you remember what the code is for.
Next, simply paste the code from above into the ‘Code Preview’ box.
After that, all you need to do is toggle the switch from ‘Inactive’ to ‘Active’ and hit the ‘Save Snippet’ button at the top of the page.
If you want to exclude multiple categories, then you can change the code like this:
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-5, -9, -23' );
}
return $query;
}
add_filter( 'pre_get_posts', 'exclude_category_home' );
Simply replace the IDs (-5, -9, -23) with your category IDs to exclude them from your WordPress homepage.
We hope this article helped you learn how to exclude a category from your WordPress homepage. You may also want to see these most wanted category hacks and plugins for WordPress that will help you use categories more efficiently, or our expert picks of the best contact form plugins for WordPress.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Jiří Vaněk
I initially used the Exclude Category plugin, but as my plugins started to accumulate, I tried your snippet, and it works just as great. Thank you very much for providing the code. It saved me another plugin that I don’t have to have in the system.
Joseph
Will using the Ultimate Category Excluder plugin block Googlebot from crawling posts in that particular category? I don’t want my site users to access a particular category.
WPBeginner Support
As long as the pages can be found in some way by Google from something like a sitemap or other page on your site then there the crawler would be fine.
Admin
Johan
As always: a huge THANKS for all your great tutorials!
Excluding a category the functions.php-way works great for me. BUT: I find that the same category is also excluded from a custom WP_Query that I put on a profile page. Why is that?
My query:
$the_query = new WP_Query( array( ‘meta_key’ => ‘fetcher’, ‘meta_value’ => $user_ID ) );
WPBeginner Support
Glad it was helpful, the code should only affect the home page, one possibility you could check would be to see if your caching could be the culprit.
Admin
Johan Hagvil
I found the solution!
Changing the second line of my snippet to:
if ( $query->is_main_query() && $query->is_front_page() ) {
WPBeginner Support
Thank you for sharing for anyone else who may be running into the same error!
metin
Let’s assume the id of the blog category is 1. Can I add php code about showing all ids greater than -2 so that it doesn’t show all categories greater than 1. Can there be a method that will work with php code that will work dynamically instead of adding an id every time?
WPBeginner Support
For what you’re wanting to achieve, it would be better to select the categories you want to show instead of trying to exclude all other categories. You can see how to do this in our article below!
https://www.wpbeginner.com/wp-tutorials/how-to-display-recent-posts-from-a-specific-category-in-wordpress/
Admin
Isaac
thanks genius this works perfectly
WPBeginner Support
Glad our guide was helpful
Admin
Abdullah Al Muaz
thanks dude. it works.
Abdullah Al Muaz
i use a specific page to display recent posts. so i think for my case it should not be /code $query->is_home /code
what should i put instead of “home”
i want to hide specific category from “blog”
WPBeginner Support
The code should work the same for your blog page if you set a page for your recent posts.
Admin
MENHADI HASAN
Hi,
How to exclude whole category and custom from the search form or can I modified search form to just search by a specific taxonomy rest all are excluded from the search
WPBeginner Support
For hiding content from your search, you would want to take a look at our page here: https://www.wpbeginner.com/plugins/how-to-exclude-specific-pages-authors-and-more-from-wordpress-search/
Admin
Sidchan Mall
Thanks for the tip, I began using the plugin to exclude theme-specific content posts (testimonials, sliders) on my website!
WPBeginner Support
You’re welcome, glad our guide could help
Admin
Paolo
I followed the Method 2 instructions, but after the change the media library doesn’t work anymore!
WPBeginner Support
The code should not affect your media library unless there is a larger error that is affecting your site. It would depend on the error message for the reason.
Admin
Christopher
Thanks for this article.
I want to ask if it’s also possible to exclude Google Adsense ads from some specific categories