# Exclude a Category From the Recent Posts Widget

> Keeps posts from the categories you choose out of the classic Recent Posts widget, for example sponsored posts or internal news.

- Type: PHP
- Runs: Run everywhere
- Category: [Widgets](https://wpide.com/library/category/widgets/)
- Tags: widgets, recent-posts, categories
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/recent-posts-widget-exclude-category/

## Code

```php
// Category IDs to leave out of the widget.
$wpide_widget_excluded_categories = array( 0 );

add_filter( 'widget_posts_args', function ( $args ) use ( $wpide_widget_excluded_categories ) {
    $args['category__not_in'] = array_map( 'absint', $wpide_widget_excluded_categories );

    return $args;
} );
```

## Notes

Use category IDs. The Latest Posts block has its own category setting.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Exclude a Category From the Recent Posts Widget" 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/
