WordPress How-To: Exclude Posts from the “Recent Posts Widget” by Category

How-to exclude posts from displaying in the “Recent Posts Widget” by specified categories in WordPress 4.1.

There are instances where you may have a category of posts that you do not want displayed in WordPress’ default “Recent Posts Widget”. The following short code sample will quickly eliminate the specified categories’ post from displaying in the “Recent Posts Widget”.

WordPress suggests that you place the following code in your theme’s functions.php file. This is not an ideal location because you will have to re-edit the file every time you update or change your theme. One suggestion is to use a plug-in like Add Actions And Filters that will allow you to maintain your action and filter edits separately from any themes. Alternatively, if you are developing your own plug-in, place the code in your plugin’s root file.

Notice on the highlighted $exclude assignment line, I have the values '-9, -10'. Place your own category ID(s) to exclude their posts here instead. Remember to include the negative symbol before your category to exclude posts from.

Once again, this will only exclude the posts from specified categories in the default WordPress “Recent Posts Widget”.

To exclude certain categories’ from displaying in the “Categories Widget”, check out WordPress How-To: Exclude a Category from the “Categories Widget”.

To exclude certain categories’ from displaying in the “Archives Widget”, check out WordPress How-To: Exclude a Category from the “Archives Widget”.

To exclude certain categories’ posts from displaying check out WordPress How-To: Exclude Posts by Category.

To exclude certain categories’ posts from displaying in posts’ “Previous/Next” links check out WordPress How-To: Exclude a Category from the “Previous/Next” Links.

Posted in WordPress Tagged with: , , , ,
8 comments on “WordPress How-To: Exclude Posts from the “Recent Posts Widget” by Category
  1. Ute says:

    Thank you so much, this really saved me from getting a heart attack 😀

  2. Tabasko says:

    Works great! But it kind of disturbs the post limit given in the plugin. I set it to a maximum of 3 posts but it now shows 9. Do you have an idea how to solve this?

  3. Richard says:

    Hi,

    This is great but as per the post above, the widget is displaying 10 posts rather than the 3 I have specified.

    Any way to solve this?

    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

*