LIMITED TIME OFFER - Save 35% OFF on Password Policy Manager. Code: 35PPMWP

How to Exclude a Category from a WordPress Blog or Page

Last updated on October 18th, 2014 by Robert Abela. Filed under WordPress Security Tutorials & Tips

If you need to exclude a category from the WordPress blog page and sidebar, you do not need to install a third party plugin and add extra administration overhead. All you need to do is follow this easy to follow step by step WordPress tutorial, and by simply modifying a file you will have the WordPress categories you want excluded in minutes. This tutorial also includes the sample code and was written specifcially for beginners or anyone without any coding experience.

Tutorial to exclude category from WordPress blog page

  1. Login to your WordPress dashboard and find the WordPress category ID of the category you would like to exclude. Read How to Find a WordPress Category ID to learn how to find out the category ID.
  2. Once logged in, also list down the name of WordPress theme you are using by navigating to the Appearance > Themes node.
  3. Connect to your WordPress using FTP and navigate to the theme’s directory. If the theme name is ‘default’, the directory of the theme is /wp-content/themes/default/.

WP White Security WordPress tip: You can modify a theme file from the built-in WordPress Editor (Appearance > Editor). Though it is always recommended to do theme and other WordPress files updates offline (via FTP like explained in this WordPress tutorial) so you can test the change and easily revert back if something goes wrong, such as a WordPress whitescreen.

  1. Download the theme’s file functions.php (e.g. /wp-content/themes/default/functions.php). Before doing any changes to the file, make a backup of such file in case you need to quickly revert back the changes.
  2. Copy the below code and paste it in functions.php file, right before the ?> (at the bottom of the file). If you want to add a comment to recognize the code at a later stage, you can do so by using the ‘//’ signs at the beginning of the comment line.

PHP code to exclude WordPress category from blog

function exclude_category($query) {
if ( $query->is_home() ) {
$query->set('cat', '-xx');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
  1. Replace the xx (mentioned in line 3 of the above code) with the category ID you recorded in step 1 of this WordPress tutorial. Leave the minus ‘-‘ sign in front of the category ID, only replace the xx.
  2. Once ready, save the file and upload it back to your website. Refresh the page and you should notice that the excluded categories are not showing up in the WordPress blog.

Excluding multiple WordPress categories from blog

To exclude multiple categories from showing up in the WordPress blog page, simply add all of the categories ID in the same line from the code above (line 3) separated by a space as per the example below.

$query->set(‘cat’, ‘-124 -125 -126’);

The example code above would exclude the categories with ID 124, 125 and 126.

WordPress Hosting, Firewall and Backup

This Website is:

21 comments

Janell 20/02/2013

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

Robert Abela 21/02/2013

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@wpwhitesecurity.com.

Oz 26/02/2013

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.

Robert Abela 26/02/2013

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@wpwhitesecurity.com.

Steve 07/03/2013

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

Robert Abela 07/03/2013

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;

< ?php function exclude_widget_categories($args){ $exclude = "ID,ID,ID"; // Specify the ID of the categories you want to exclude $args["exclude"] = $exclude; return $args; } add_filter("widget_categories_args","exclude_widget_categories"); ?>

Silvia 22/03/2013

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?

Robert Abela 25/03/2013

It is quite difficult to say unless we look into your setup. Drop us an email on support@wpwhitesecurity.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.

Silvia 28/03/2013

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

Robert Abela 29/03/2013

Glad you solved it 🙂 As regards your last query I didn’t understand. Maybe you can explain in more detail?

Silvia 29/03/2013

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.

Robert Abela 30/03/2013

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/

Simon Mogren 13/05/2013

Thanks! It works like a charm!
So nice to have this feature.

Alexander 20/11/2013

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

Robert Abela 23/11/2013

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.

Franz 10/07/2014

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

Robert Abela 16/07/2014

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 🙂

Toke 08/08/2014

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.

Robert Abela 11/08/2014

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@wpwhitesecurity.com.

Looking forward to hearing from you.

Art 18/11/2015

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?

Robert Abela 24/11/2015

Hello Art,

Unfortunately I cannot tell you offhand since we haven’t worked on themes in ages. Though drop us an email on support@wpwhitesecurity.com and we can talk about it.

Leave a Reply

Your email address will not be published. Required fields are marked *