How to exclude specific post categories from the recent posts widget in Wordrpess

In this stackoverflow q&a post, you can find how to edit the recent posts widget to only include certain categories.

I wanted to exclude certain categories from the recent posts widget. I tried several methods found in the Internet, but in vain. I tried to utilize the following function which only includes certain categories in the recent posts widget.

add_filter('widget_posts_args','modify_widget');

function modify_widget() {
$r = array( 'cat' => '3' );
return $r;
}

I used 'category__not_in' to exclude certain categories which worked well:

add_filter('widget_posts_args','modify_widget');

function modify_widget() {
$r = array( 'category__not_in' => '3');
return $r;
}

Where "$r = array( 'category__not_in' => '3');", please change the number to the category id number you want to hide from the recent posts widget.

For multiple categories, please use the following code instead of "$r = array( 'category__not_in' => '3');"

$r = array( 'category__not_in' => array(3, 4));

 

As you know, please copy the above function and paste it into the functions.php under your theme folder.


Leave a Comment