WordPress How-To: Exclude Posts by Category

How-to exclude certain categories from displaying their posts in WordPress 4.1.

There are instances where you may have a category that you do not want its posts to display with the rest of the site’s posts. The following short code sample will quickly eliminate the listed categories from displaying their posts in the following locations:

  • On the Homepage
  • In the Recent Posts Widget
  • Anywhere Posts are listed by Category

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.

In my example I have eliminated the posts from showing up on all pages except for the “admin” page. I checked for this condition with the line if( ! is_admin() ). You may put any conditions you need for your requirements here as well. For example, if you wanted to also include a specific page with ID 90, try something like:
if( ! is_admin() && ! is_page( 90 ) )

Once again, this will only exclude one or more categories’ posts from displaying on the Homepage, Recent Posts Widget, and any lists of posts organized by categories. Please note, that an excluded categories’ posts will still be present in the “Previous” and “Next” post feature, as well as be counted in the “Archive Widget” and “Categories Widget” tallies.

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’ from displaying in the “Recent Posts Widget”, check out WordPress How-To: Exclude Posts from the “Recent Posts Widget” 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: , , , ,

Leave a Reply

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

*