Categories
WordPress Tips

How To Exclude Posts In Specific Categories From Displaying

There’s different ways you can exclude all posts in one or more categories from displaying on your home and/or blog pages.

We’ll be using pre_get_posts

pre_get_posts should be used in custom functions. (child themes)

You can:

  1. Install a plugin
  2. Add a custom function to your child themes functions.php file

Demo Video #

Shows all posts in category 9 which is the lifestyle category, removed from the front page loop.

Add Code To Themes Template Files

You could also add the above code to any theme which uses an index file like one of the default WordPress themes. Preferably not in the parent theme.

Another option would be to add this code into your home.php file within the loop.

pre_get_posts & Category_Parameters Function

Use this code in your child themes functions.php file.

This is the best method in my opinion.

You can change the category parameters and conditional tag to suit your needs.

Or this method :

This code works with Genesis theme framework and excludes posts from category 27 and 30 from displaying on the home page.

It wouldn’t be difficult to change the is_home conditional tag and replace with your own category i.d’s.

Related Tips

10 replies on “How To Exclude Posts In Specific Categories From Displaying”

On which page? For your front page? Are you using a front-page.php template? Or have you changed the Reading settings for the front page displays? The genesis blog page template has been removed.

Thanks for sharing.

Yes, you simply remove the minus sign before each category I.D

$query->set( 'cat', '27,30' );

Hello Brad,

I thank you so much for this usefull code, it took me hours of successless try’n’rollback, before I found your snippet. I’m really glad about your help, one big point on my todo-list is now marked as “cleared”.

Thank you!

Regards
Christian

Hello,

Can you please let me know that how can we display the “Author box” and the social media sharing button in the first post when we go to category and tags with full post.

Currently the full text is coming but Author box and social media sharing buttons are not coming. i am using Genesis theme. Please let me know how can we do that. It is urgent.

Thanks

Leave a Reply

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