Sometimes, you don’t want a certain category of posts to appear on your homepage or in other areas of your sites but still appear in their own category pages.
Maybe you only want to showcase your most popular topics, or simply want to declutter and organize your blog. Or, maybe you have empty categories you want to keep for a rainy day but don’t want them listed. Is deleting or unlisting your posts really the only option?
The answer is no, thankfully.
You can just hide categories from your blog pages. That way, any post within a certain category will no longer appear in your main blog view but will still be accessible in their own category pages. Unfortunately, WordPress has no built-in way to do this. You’ll need to either add custom code or install a plugin.
There are multiple ways to exclude all the categories you’d like from your WordPress site, and we’ll cover exactly how to do it below: with a code snippet, a plugin to hide categories in WordPress, or even password-protecting categories so no one can access them.
With this small change, your website will be more streamlined and organized. Let’s get right into it.
Table of contents
Exclude categories with PHP
If you want to exclude a WordPress category, or several categories, from your homepage, sidebar, and blog page, a simple code snippet will do the trick.
Here is the PHP code:
function exclude_category( $query ) {
if ( $query->is_home ) {
$query->set('cat', '-xx');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
You’ll need to replace the “xx” on line 3 with the proper category ID, but we’ll explain that in a moment.
If you want to exclude several categories, you can just add each one separated by a space. Here’s an example:
$query->set('cat', '-1 -2 -3');
So, how can you find a WordPress category ID? Just go to the category page at Posts > Categories and click on the category you want to exclude. In the address bar, you’ll see something like this:
yourwebsite.com/wp-admin/term.php?taxonomy=category&tag_ID=7
Check the tag_ID in the address bar. This is your category ID. Replace the “xx” in the code with this number.
Here’s how this code works:
- Any post that falls under the categories you set will be removed from most parts of the site. For example, it will be removed from your homepage where your posts are shown, widgets in your sidebar and footer, and so on.
- Posts from excluded categories will still appear in search results, on their own WordPress category page, and will be indexed by search engines.
- If you have a widget that lists all your categories, the categories you exclude won’t be hidden from that category list.
That way, posts from excluded categories can still be found, but they won’t clutter up your more important and relevant blog posts. If you want it to work differently, you can add to the code yourself if you know PHP or try using a category excluder plugin. We’ll go over that later.
Now, let’s add this code snippet to your website.
How to add code with a plugin
The easiest way to add small bits of code like this is with a code snippets plugin. We’ll use WPCode.
1: Open your WordPress dashboard and go to Plugins > Add New Plugin. Search for WPCode, then install and activate the plugin.
2: You’ll have a new menu item on the left: Code Snippets > + Add Snippet. Once you open this, hover over Add Your Custom Code (New Snippet), and click the + Add Custom Snippet button.
3: Click the Code Type dropdown and select PHP Snippet. Copy the PHP code snippet we provided in the section above and paste it into the Code Preview box. Make sure you replace the ‘-xx’ in the code you just pasted in with the category IDs as explained above.
4: Optionally, you can scroll down and use the Location picker to place this code only in certain parts of your site or set up Smart Conditional Logic for it if you want your excluded category to be viewable under certain conditions.
5: Make sure you tick the capsule next to Inactive to activate your snippet, then click Save Snippet.
With that, you should notice posts of the categories you set are no longer showing.
How to add code manually
If you don’t want to rely on a plugin, you can also add this code snippet manually to functions.php. There are two ways to do this: via FTP (File Transfer Protocol), or directly through the dashboard.
There are actually two functions.php files: one for WordPress core (located in the wp-includes folder), and one for your active theme (located in the /wp-content/themes/ folder). It’s generally recommended to avoid editing core files, especially since they’re often reset when WordPress is updated or reinstalled, so we’ll be editing the file related to themes.
What follows are two approaches for completing this task.
Edit functions.php through the Theme File Editor
One way to add this code is to edit functions.php through the dashboard. You can find the file editor in Tools > Theme File Editor. Just look for Theme Functions (functions.php) in the list on the right, then paste your code in at the bottom.
Note that rather than editing theme files directly, it’s recommended to create a child theme and edit that instead. This ensures that your customizations won’t be lost when your theme updates. To make sure you’re using a child theme, check your active theme under Appearance > Themes in your WordPress dashboard. If it’s not a child theme, you can create one by following a few simple steps, as outlined in the official Theme Handbook.
Edit functions.php through cPanel
Another way to edit this code manually is to access functions.php through cPanel in your hosting account. Not all hosts allow this, however, as they don’t all provide users with FTP/SFTP access. If you don’t have this level of access, just follow the previous process, editing through WordPress. To access your website files through FTP, you can:
1: Obtain your FTP credentials. If your web host uses cPanel, you can find your credentials under the Files tab in the FTP Accounts section. Otherwise, you can ask them for your FTP credentials.
2: Install an FTP client like FileZilla. This will allow you to connect to your website via FTP and edit its files. After installing the FTP client, open it.
3: Enter your FTP credentials into the bar at the top (Host, Username, Password, and Port), and click Quickconnect.
4: You can find your theme’s functions.php in the wp-content/themes folder. Open your active theme. Double-click functions.php to open and paste your code at the bottom. Save the file and upload it back to your website.
Hide categories with a plugin
Sometimes it’s good to write your own code rather than installing a bunch of unnecessary plugins, but if our PHP snippet doesn’t quite do what you want it to, you’ll need a plugin like Ultimate Category Excluder.
This plugin allows you to configure exactly what categories you exclude and where you exclude them from, including from your homepage, archives, RSS feeds, and search results. You can set these for each individual category for maximum customization.
Despite the granular level of control, this plugin is super simple and easy to use, with only a single settings page with boxes to tick.
Here’s how you can use Ultimate Category Excluder to hide WordPress categories from blog pages.
1: In your WordPress dashboard, go to Plugins > Add New Plugin to install and activate Ultimate Category Excluder.
2: You’ll find a new menu tucked away in Settings > Category Excluder. Now you can set which categories you want to exclude from various parts of your site.
3: When you’re finished, click Update to save.
If you’re not sure what these settings mean, here’s an overview.
- Exclude from front page – This will remove posts not just from your homepage, but also any widgets that list your posts. For example, if you have a “Latest Posts” widget in your footer, excluded categories won’t appear there on any page.
- Exclude from feeds – This will hide categories from your RSS feed.
- Exclude from All Archives – This will hide categories from your full list of archived posts. For example, if you use the Archives block, any selected posts won’t appear in the list.
- Exclude from Search – Excluded categories will not appear in WordPress search results.
Note that this will not hide specific categories from search engines like Google – these will still be indexed by search engines and can be found by organic visitors. If you want to hide a post from your website entirely, it’s best to unpublish it. You can password-protect the page to prevent casual visitors from accessing it, but search engines may still index it. To hide the page from search engines, you should either disallow crawling and indexing via your site’s robots.txt file or use a “noindex” meta tag.
Password protect WordPress categories
Another way to hide categories and the pages within them is by adding password protection, ensuring that only authorized users can access them.
Natively, WordPress does support password-protecting an individual posts’ page, but not categories or all posts within them. All you need to do is open any post and click the Settings button (an icon of a box) in the top right corner. Click Status, and tick Password protected to set a password.
You may wish to combine this with the category exclusion steps above, as password-protected posts will still appear on the front page.
If you want to password-protect all posts in a category at once, you’ll need a plugin like Password Protect WordPress. Here’s how to do it.
1: Install and activate PPWP (Password Protect WordPress) through Plugins > Add New Plugin.
2: Now go to Posts > Categories. You’ll see a new section: PPWP Lite: Category Protection. Tick the toggle button and select the categories you want from Select your private categories.
3: Click Save Changes.
4: Now, you may want to make it so your password-protected pages don’t appear on certain pages, like the homepage. Navigate to Password Protect WordPress > Settings.
5: In the Protected Content Visibility section, tick the Exclude the password protected posts from the following views button. You can click the X to remove any pages or click in the box to add more.
6: Click Save Changes when you’re done.
If this doesn’t work, you may need to add some code to hide password-protected categories, or you can use Ultimate Category Excluder as outlined in the steps above.
Organizing WordPress categories
Making your content truly resonate with its intended audience matters a lot, and a little organization can surprisingly help you achieve this. Hiding WordPress categories from your blog page is a good way to organize and declutter your site, ensuring that only your best content makes it to the front page – but still draws in organic traffic.
Whether you use plugins, add custom code, or resort to password-protecting your categories, your content is now a little more streamlined and easier to navigate.
Hi Robert,
What should I use instead of “is_home” if I want to hide the categories from the category widget that I place on my sidebar?
Hello Art,
Unfortunately I cannot tell you offhand since we haven’t worked on themes in ages. Though drop us an email on support@melapress.com and we can talk about it.
Unfortunately after implementing this code in my functions.php, the category still shows in post page. Please any solution? I want the category to disappear from my blog, post page, widget or anywhere I decided to display all categories. Waiting for your reply.
Hi Toke,
Thank you for following our plugin. Unfortunately offhand we cannot really tell what is working or not, since such functionality depends a lot on the theme. If you would like us to assist you further, please send us an email on support@melapress.com.
Looking forward to hearing from you.
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?
Indeed Metin, it is possible to achieve what you want with a bit of PHP code. If you are not familiar with coding, you can always find a developer to help you with it. All the best with building this.
Hi, nice function!
But this function excludes also the categories on pages.
Is there a code that works also on pages?
I mean to exclude on the Homepage, but not on another page?
Kind Regards
Alex
Hi Alexander,
Sorry for the late response.
You can use the same concept code to use it in pages. For example instead of
if ( $query->is_home() )
you can use something like
if ($query->is_page(3) )
Where 3 is the page ID. Of course then you have to use different filters for when the page loads up the content.
I hope the above answers your queries. Should you have any further queries, just get in touch with us via our contact form.
Hi,
but what happens, if there is no page ID?
http://www.website.eu/wp-admin/post.php?post=2&action=edit
On this page I want to exclude category 20.
Kind regards,
Franz
What do you mean there is no page ID? Every object in WordPress has an ID. Drop us an email or contact us from our contact form about this. We will help you sort it out 🙂
Thanks! It works like a charm!
So nice to have this feature.
Thanks for the code been looking for something like this. I have a specific category that I´ve created a category-slug page for, so I dont want it to show in the normal archives page. Can I edit this code to get that to work, or does this just work with the homepage?
It is quite difficult to say unless we look into your setup. Drop us an email on support@melapress.com and we will gladly assist you. If need be we simply write a new small function specifically for your setup.
Looking forward to hearing from you.
thanks for the offer. I managed to solve it. this stops the cat being shown on all archive pages.
function exclude_category( $query ) {
if ( $query->is_archive() && $query->is_main_query() ) {
$query->set( ‘cat’, ‘-388’ );
}
}
add_action( ‘pre_get_posts’, ‘exclude_category’ );
Now all I need to work out is how to add cat 388 back in on the specific template. hehe
Glad you solved it 🙂 As regards your last query I didn’t understand. Maybe you can explain in more detail?
I have one category (bios) that I want to display differently to the rest of the site. So I removed bios from the archive pages with the code shown in my post above, and I set up a specific Archive Template for just that one category. I now just need to solve how to show the bios on that one page. Because atm they dont show because of the pre get post I set in functions.
I can´t seem to find anwhere if I can use a specific category in this call
if ( $query->is_archive() instead of archive.
or maybe as I removed the cat wit pre get posts I should add it back in just on that page in the loop with get query.
I suppose the other option is just to set up a custom post type for the bios.. probably easier. I´m working off the hybrid framework, so not all the info out there works with the theme anyway. Haven´t looked into the custom post options on the framework yet.
Hi Sylvia,
Use conditional statements and use both is_archive and is_category and you should be able to display the archive of a specific category. I am sure you can find more information on the WordPress Codex.
Alternatively you can use a plugin to achieve what you want; http://wordpress.org/extend/plugins/wp-category-archive/
Thanks for the tutorial…
I couldn’t get it to work…
I found the category id and it was removing the “exclude” category from my homepage…so I tried another ID and when I tested it, the post disappeared…ok…hmmm Looks like User Error…
What I need is to removing the category from the “category” widget…and a post…the reason for this is because…I set up a category called “exclude” and assigned the TOS, Privacy Policy etc. to this category.
Now when I go to the site, the category “exclude” shows up in the “category” widget…
How would a Pro like you do this, ie., exclude a category from the “category” widget?
Thanks in advance,
Steve
Hi Steve,
There are plugins which you can use to exclude categories from a Category widget. Alternatively you can add this code to the theme’s file functions.php to exclude the category from the category widget;
Question: can I load the code into my custom CSS area?
My concern is if I add the code directly into my theme, the next update will was away any customization.
Hi Oz,
Yes unfortunately it is something you have to update each time you update the theme. Having said that, the theme do not frequently change, especially the functions.php file. The easiest way to find if the functions file changed is to use some text file comparison tool and compare both versions of the file functions.php.
If you have any further queries or need assistance regarding this issue, do not hesitate to get in touch with me directly on robert@melapress.com.
Hi,
Thank you so much for taking the time to write this up. I have been all over the internet trying to exclude one of the categories as I am using WooSlider using the Post function as I want the photos to show up with SEO friendly text/captions, but do not want those photos as posts on the Post page. Your code worked perfectly.
Again, thanks so much!
Cheers,
Janell
Hi Janell,
We are glad our tutorial was of help. If you ever need assistance or would like to know more about something in particular, or want to see a particular article, drop me an email on robert@melapress.com.