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.
Opens WPIDE on
Code
// 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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.