Search
Close this search box.

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.
Table of Contents

Sponsored Ad

If you purchase through a link on our site, we may earn a commission.

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.

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.

Sponsored Ad

If you purchase through a link on our site, we may earn a commission.

All suggestions are anonymous.

More from our blog...

50 Responses

  1. How will this code change if I want:
    -> Users to register
    -> Select categories they prefer / or exclude those they don’t (Similar to Spotify sign up process)
    -> Save to user profile
    -> Home page for them is now personalised to their categories they have chosen
    -> and every time they log in it remembers their custom home page

    Much like the plugin, but personalised for each user (when logged in), so different home page for every user based on their selection.

  2. 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

  3. 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?

  4. 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

  5. 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.

  6. My brother recommended I would possibly like this blog.
    He wwas totally right. This postt actually made my day.
    You can not consider simply how a lot time I hhad spent for this information! Thank you!

  7. 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!

  8. 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.

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

  10. 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.

  11. You explained about adding the code and it’s perfect but where exactly should I enter the code? Should it before <?php code or not.

  12. 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 🙂

  13. 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?? 😉

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

  14. 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

  15. 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?

  16. 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

  17. 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!

  18. Thanks! You just saved my day. I have been messing around with this problem for a while.

  19. 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! :)

  20. 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

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

  22. 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.

  23. 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.

Post a Comment

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

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

Stay updated with WP Mayor's newsletter showcase every week

Stay on top of every new WordPress innovation and latest launches. Receive all our fresh product reviews and expert guides directly in your inbox.

Hosting Survey 2024

Are you happy with your hosting provider or are you over-paying for too little? Have your say below!

"*" indicates required fields

What's the main reason you picked this host?*
How happy are you with your host?*

OPTIONAL: If you'd like to receive the results of this survey, please enter your details below.