Title: Exclude Category using Child Theme
Last modified: December 20, 2017

---

# Exclude Category using Child Theme

 *  [Anna1226](https://wordpress.org/support/users/anna1226/)
 * (@anna1226)
 * [8 years, 5 months ago](https://wordpress.org/support/topic/exclude-category-using-child-theme/)
 * I feel like I’m doing everything with best practices and it’s not working.
 * I created a child theme, so as not to affect the original functions.php.
 * I know my child theme functions page is being “read” cuz when I have a typo, 
   it gives me an error message.
 * Here’s my exclude function, copied & pasted directly from the codex page.
 * *******************************************************************
 *     ```
       function exclude_category( $query ) {
           if ( $query->is_home() && $query->is_main_query() ) {
               $query->set( 'cat', '-17,-18' );
           }
       }
       add_action( 'pre_get_posts', 'exclude_category' );
       ```
   
 * *******************************************************************
 * Yes, I’ve double-checked that the category numbers are correct. Yes, I’ve tried
   the opposite function, to display just one category, also didn’t work. (i.e. 
   all categories continue to display.)
 * Why isn’t it excluding those two categories (advertisers, testimonials)?
    -  This topic was modified 8 years, 5 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fexclude-category-using-child-theme%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [8 years, 5 months ago](https://wordpress.org/support/topic/exclude-category-using-child-theme/#post-9800652)
 * If this is the only thing the child does, you could using this plugin with the
   parent instead: [https://wordpress.org/plugins/ultimate-category-excluder/](https://wordpress.org/plugins/ultimate-category-excluder/)
 * I think the code should be
 * `$query->set( ‘cat’, array(-17,-18 );`
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [8 years, 5 months ago](https://wordpress.org/support/topic/exclude-category-using-child-theme/#post-9800948)
 * It may be related to how your site is setup and/or how your theme manages the
   home page content. If you have your site set to use a static front page, your
   query adjustment will only be applied to the blogs page, not the front “home”
   page.
 * If your theme is running some other sort of query to generate home page content,
   so that the query used is not the “main” query, your code is ignored. This is
   more common than you may realize, even though such themes are following poor 
   practice. If the theme uses an SQL query to get posts, the pre_get_posts action
   will not even fire. Your code needs to work with your theme. Code pulled from
   the Codex is typical, but not necessarily applicable.
 * It’s also possible some other pre_get_posts hook is overriding or negating your
   code. These can be difficult to track down. First try removing the conditionals
   from your code. It will be applied for queries where it’s undesirable, but it
   will at least confirm the set value is appropriate and that pre_get_posts fires.
 * Then try the conditional is_front_page() instead of is_home() — without the main
   query check for now. If that works, try adding the main query check back in. 
   If any of these steps fail to work, we’ll need to look at how the theme is generating
   the query.
 * Passing cat arguments as a string value is acceptable, but I would advise using
   array syntax exclusively. There are some query vars where only array syntax will
   work. Mixing array and string syntax is a bad idea IMO. The proper array syntax
   is
    `$query->set('category__not_in', array( 17, 18,));`

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Exclude Category using Child Theme’ is closed to new replies.

## Tags

 * [exclude categories](https://wordpress.org/support/topic-tag/exclude-categories/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 3 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [8 years, 5 months ago](https://wordpress.org/support/topic/exclude-category-using-child-theme/#post-9800948)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
