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


Comments

10 responses to “How To Exclude Posts In Specific Categories From Displaying”

  1. Kevin Eberle Avatar
    Kevin Eberle

    This no longer works with the most recent Genesis updates.

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

    2. What you can do is create a page named blog and set that as your posts page in your Reading settings. Then you can use a front-page.php file for your front page.

  2. Brad,

    Thanks for this. I have 30 categories. I’m wondering, is there a way to only show just one category instead of excluding many?

    1. Hmmm. Maybe this would do it?
      [code]
      function my_home_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( ‘cat’, ’11’); } } add_action( ‘pre_get_posts’, ‘my_home_category’ );
      [/code]
      Forgive the messy code layout.

      YEP! It works.

      If you want to include more use this syntax [code]$query->set( ‘cat’, ’11, 14’ );[/code]

      I grabbed this info from https://premium.wpmudev.org/blog/one-category-wordpress-homepage/

      1. Brad Dalton Avatar
        Brad Dalton

        Thanks for sharing.

        Yes, you simply remove the minus sign before each category I.D
        [code]$query->set( ‘cat’, ‘27,30’ );[/code]

  3. Chris Praetorius Avatar
    Chris Praetorius

    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

    1. Brad Dalton Avatar
      Brad Dalton

      No worries Chris.

      Thanks for the positive feedback.

      Merry Xmas!!

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

    1. Brad Dalton Avatar
      Brad Dalton

      Have you asked this question on the StudioPres forums?

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.