Mastering how to exclude categories wordpress: A Step-by-Step Guide

Why Excluding WordPress Categories is a Powerful Tool

WordPress categories are the backbone of a well-organized website. They help you structure your content, guide visitors, and improve your site’s SEO. However, there are times when you might want to hide or exclude specific categories from appearing in certain areas of your site. Perhaps you have a “Personal Musings” category you don’t want on your business homepage, or an “Archived Projects” category cluttering your main blog feed. Learning how to exclude categories in WordPress is an essential skill for tailoring your site’s presentation and creating a focused user experience. This guide will walk you through several effective methods, from simple settings to custom code.

Understanding Where Categories Appear

Before you start excluding categories, it’s important to know where they typically show up. The most common places include:

  • Your main blog page or posts page
  • Category archive pages
  • Sidebar or footer widgets (like “Recent Posts”)
  • Navigation menus
  • RSS feeds
  • Search results

The method you choose will depend on where you want to exclude the category and your comfort level with different WordPress tools.

Method 1: Using a Dedicated Plugin (Easiest)

For most users, especially beginners, a plugin is the fastest and safest way to exclude categories. It requires no coding and offers a user-friendly interface.

Recommended Plugin: Ultimate Category Excluder is a popular, lightweight, and well-maintained option.

  1. Install and activate the “Ultimate Category Excluder” plugin from your WordPress dashboard (Plugins > Add New).
  2. Navigate to Settings > Category Exclusion.
  3. You will see a list of all your categories. Simply check the boxes for the categories you wish to exclude from your homepage, feeds, archives, and search results.
  4. Click Save Changes.

This plugin gives you granular control, allowing you to exclude a category from one area (like the homepage) while still allowing it to appear in another (like search results).

Method 2: Customizing the WordPress Query with Code (Intermediate)

If you prefer to avoid adding another plugin or need more customized control, you can modify the main WordPress query using code. This method involves adding a snippet to your theme’s `functions.php` file.

Important: Always use a child theme when editing theme files. This prevents your changes from being overwritten during theme updates.

Here is a basic code example that excludes a category with the ID of 7 from your main blog page and feed:

function exclude_category_from_blog( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-7' );
    }
}
add_action( 'pre_get_posts', 'exclude_category_from_blog' );

How it works: The `pre_get_posts` action hook lets you modify the query before it runs. The `cat’ => ‘-7’` parameter tells WordPress to get all posts except those in category ID 7. You can find a category’s ID by going to Posts > Categories and hovering over the category name—the ID will appear in the link in your browser’s status bar.

To exclude multiple categories, separate the IDs with commas: `’cat’ => ‘-7, -13, -19’`.

Method 3: Excluding Categories from a Specific Widget

What if you only want to exclude categories from a “Recent Posts” widget in your sidebar? The default widget doesn’t offer this option, but you can easily achieve it.

  • Plugin Approach: Use a more advanced widget plugin like Recent Posts Widget With Thumbnails, which often includes category inclusion/exclusion filters.
  • Custom Widget Code: If you’re using a theme that supports it, you can sometimes find a theme-specific “Recent Posts” widget with extra options in the Customizer.

Method 4: Manual Exclusion in Theme Templates (Advanced)

For developers or those with specific theme template needs, you can edit the loop in your theme files. For instance, to exclude a category from a custom page template, you might modify the query arguments directly within the template file (`page-template.php`).

Example snippet within a template:

$args = array(
    'category__not_in' => array( 7, 13 ),
    'posts_per_page' => 10
);
$custom_query = new WP_Query( $args );

This method offers maximum flexibility but requires a solid understanding of WordPress theme development and the WP_Query class.

Best Practices and Considerations

When excluding categories, keep these points in mind:

  • SEO Impact: Excluding categories from archives or feeds does not delete them. The posts and their category archive pages remain accessible via direct URL. This is generally fine for SEO, but if you want to completely remove content, consider deleting it or using a proper redirect.
  • User Experience: Ensure your exclusions make sense. If a user clicks on a link to a category, they should still see its posts. Exclusions are typically for “global” views like the homepage.
  • Backup First: Always back up your site before adding plugins or editing code.
  • Test Thoroughly: After making changes, check your homepage, blog page, widgets, and search results to ensure the exclusion is working as intended.

Conclusion: A Cleaner, More Focused Website

Mastering how to exclude categories in WordPress empowers you to curate your site’s content presentation precisely. Whether you choose the simplicity of a plugin like Ultimate Category Excluder or the precision of custom code using `pre_get_posts`, you have the tools to remove clutter and highlight the content that matters most to your audience. By strategically hiding irrelevant or outdated categories, you enhance the user journey, maintain a professional appearance, and ultimately create a more effective website. Start by identifying one or two categories that don’t fit your primary site goals and use the method that best suits your technical comfort level to hide them today.

Leave a Comment