# Hide Subcategory Posts From Parent Categories

> Makes a category archive list only the posts assigned directly to that category, not the posts from its child categories.

- Type: PHP
- Runs: Run everywhere
- Category: [Archives](https://wpide.com/library/category/archive/)
- Tags: categories, archives, subcategories
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/exclude-subcategory-posts/

## Code

```php
add_action( 'parse_tax_query', function ( $query ) {
    if ( is_admin() || ! $query->is_main_query() || ! $query->is_category() || empty( $query->tax_query->queries ) ) {
        return;
    }

    foreach ( $query->tax_query->queries as $index => $clause ) {
        if ( is_array( $clause ) && isset( $clause['taxonomy'] ) && 'category' === $clause['taxonomy'] ) {
            $query->tax_query->queries[ $index ]['include_children'] = false;
        }
    }
} );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Hide Subcategory Posts From Parent Categories" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
