WordPress Exclude Categories from main pages

Today I wanted to restructure this website, by excluding the Articles from Blog Section… I using worpress (Yeah, I am Lazy..) . So It turns out there is an easy solution for that:

1. Go on Posts->Categories .

2. Hover the mouse over the categories that you want to hide from the main blog page and look at the ID in the shown URL.

3 Now that you have the category ID you can edit the PHP file.. Go on Apperance–>Editor, and locate the “functions.php” File of your Template.

4. Add this line of code in the end, substituting the categories that you want to exclude. In the example below i am excluding the categories 21 and 29.  You can exclude more categories by adding more comma separated values.

#function exclude_category( $query ) {
  if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-21, -29' );
        }
  }
add_action( 'pre_get_posts', 'exclude_category' );

5. Save and reload… The selected categories now should not appear anymore on the main blog page.

ExtraTip: If you write 21, 29  (without the minus sign) then you will ONLY show the categories 21 and 29 in the main page (hiding all the others).

I hope that was useful!

Click to rate this post!
[Total: 1 Average: 5]

Leave a Reply

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