WordPress Black Friday & Cyber Monday Deals 2021
Days
Hours
Minutes
Seconds

How To Hide or Remove Categories from a WordPress Homepage

When displaying your latest posts on the homepage, you might want to exclude some categories from displaying on this page. There are many reasons why you would want to do this. You might want to keep your homepage for only news posts and relegate the rest of the posts to other sections accessed through the menu.

When displaying your latest posts on the homepage, you might want to exclude some categories from displaying on this page.

There are many reasons why you would want to do this. You might want to keep your homepage for only news posts and relegate the rest of the posts to other sections accessed through the menu.

In any case, what we will need to modify is the WordPress loop. All WordPress themes make use of the loop, and the best way to modify it is by using the pre_get_posts hook.

Removing categories from the homepage can sometimes turn out to be quite a frustrating act for beginners, so here’s the easy way to do it.

In your theme’s functions.php file, just insert the following code, replacing the category IDs with the ones you want to exclude:

function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-5, -34' );
}
return $query;
}

add_filter( 'pre_get_posts', 'exclude_category_home' );

If you’re not familiar with how to obtain the category IDs, I recorded a short clip showing you how you can easily get the IDs for the categories you want to exclude:

Remove Categories from Homepage using a Plugin

If you’re absolutely not comfortable with editing your theme’s files, there is also a plugin alternative you can use.

Ultimate Category Excluder

ultimate category excluder

Ultimate Category Excluder is a WordPress plugin that allows you to quickly and easily exclude categories from your front page, archives, and feeds. Just select which categories you want to be excluded, and UCE does all the work for you!

Get Ultimate Category Excluder

From the two plugins, I prefer Ultimate Category Excluder, it’s a simpler plugin and works perfectly for most needs. Another advantage when using a plugin solution is that you can easily remove categories not only from the homepage but also from other sections of your site.

In case you’re looking for a powerful WordPress hosting and a knowledgeable support team that can help you easily with any hosting issue, check out SiteGround.

Jean Galea
Jean Galea
Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Consider sharing this post so others can find it:

Share on facebook
Share on twitter
Share on linkedin
Share on reddit
Share on telegram
Share on whatsapp
Share on pocket
Share on email

Join thousands of people receiving real-world, genuine evaluations of WordPress products and services just like this one every week.

Our Sponsors
Solid Affiliate
Contents

49 Responses

  1. Hi, Can this piece of code be implemented with just the basic WordPress.com CSS upgrade?

    Being a novice, I’m not sure if this article is about .org sites or .com sites.

    Thanks

    Adam

    1. No the code above would not work for WordPress.com because it is not CSS code. The code above is PHP code which is the programming language WordPress uses. CSS is (mostly) a styling language so very different from a programming language which gives WordPress it’s functions. Hope that helps for anyone seeing this in the future.

  2. I have the same question as adamjameswoods — does this work (and if so where do I put in it) for a basic wordpress.com site with the Custom Design Upgrade.

  3. My website goes blank after I paste the code in the functions.php file. Not cool x)

  4. There are errors in the code above, but I can’t write the corrections here because the page refuses to render them.

    All instances of ampersand followed by lowercase g followed by lowercase t followed by semi-colon should be replaced by a hyphen followed by a greater-than sign

  5. Hi, thank you for sharing your teachings. I’m trying to hide posts from unsubscribe members along my site with out display restriction information or any register form. I’ve been searching a plugin but no success so far. Any idea? Thanks again! :)

  6. I messed up my ENTIRE blog today taking this advice. DO NOT copy and paste this code, and best practice is to just not touch PHP in general I’ve not learned. “Just insert the following code…” DONT!

  7. Hi,

    Great piece of code, thanks for it!

    I was wondering if this query could be changed to INCLUDE posts with a certain tag.
    Like, show on the homepage only the posts having the “home_page” tag, etc. ?

    Thanks,

    RP

  8. I have tried it and it doesn’t work.
    I have my post page in a different page not home page.
    I substituted home for page(IDcategory)

    it didn’t work, any suggestions?

  9. HI, just wondering, I was able to exclude one category this way. But when I added a second, the new one shows on the home page ;(
    Is there anything else I could try?
    thanks

    1. Hey Mya, you simply need to add the code above to the end of your theme’s functions.php file.

  10. Installed Ultimate Category Excluder, thank you for your post.

    i saw that others are able to post with their website but there is no option for me to write website link, why?? 😉

  11. Tried this just now and works great. An explanation or breakdown of the code would help people understand it better though instead of just copying and pasting. Would make this post more valuable.

    1. Glad you found it useful Eddie. What would you break down exactly? There isn’t much to the code, but if we can improve the explanation, we’re all ears 🙂

  12. Glad to have this article.It is really useful to me. I want to rename Uncategorized category in my WordPress site. Uncategorized category is really unprofessional and unskilful. That’s why I want to rename with another meaningful category. Can you tell me how to do it. Thanks for your article.

  13. Thanks for posting this article….It really helped to exclude a post from my homepage despite trying many options. Once again, thanks so much.

  14. I was facing category option problem but after visit this site and read full article i have solved my old problem and thanks for sharing useful article.

  15. Thanks for the reply. I just looked where you suggested but still found no way to remove the category link, so I’ll go post where you suggested. Thanks again!

  16. The best way to hide categories is through the use of control panel of your theme. Otherwise you have to know how to modilfy a php page.

  17. i get an error when i do that and the plugin does not work for me since the script/theme has it’s own categories, so the plugin does not see them

    Warning: Cannot modify header information – headers already sent by (output started at /home/pacayo5/public_html/wp-content/themes/wpjobster-child/functions.php:1) in /home/pacayo5/public_html/wp-content/themes/wpjobster/includes/functions/functions-currency.php on line 12

  18. Thanks for your useful guide. I can follow and hide categories from my homepage easily. Can you also recommend a solution to remove archives categories?

  19. It would be great if there were an option to exclude category page/post.
    So, it would be possible to have a personalized page with a category name/slug

Leave a Reply

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

The Beginner’s Handbook
From an introduction on how WordPress works to our recommendations on products and services.
👋 Hey there! We're Gaby and Mark
Every week we share tutorials and genuine reviews of WordPress products and services in our newsletter.
Thousands of people read it!
We’d love for you to join.
We’d love for you to join. Here’s what you’ll be getting:

A single weekly email directly to your inbox.