User Agents

Web browsers and everyday web tech, explained

Exclude WordPress Category from Homepage Guide

· 5 min read · Settings and Privacy

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Excluding a specific category from your WordPress homepage allows you to keep internal or administrative posts, such as "Private Updates" or "Test Posts," hidden from your main feed while keeping them accessible via direct links or category archives. By default, WordPress displays all published posts on the homepage regardless of category; however, you can change this behavior by adding a small snippet of code to your theme's functions.php file or by using a dedicated plugin.

What You'll Need

  • Administrator access to your WordPress dashboard.
  • The ID number of the category you wish to exclude.
  • Access to your site files via an FTP client (like FileZilla) or the WordPress Theme File Editor.
  • A recent backup of your website to prevent errors during code implementation.

Step-by-Step

  1. Find the Category ID: Log in to your WordPress dashboard. Navigate to Posts > Categories. Hover your mouse over the category you want to exclude. Look at the URL displayed in the bottom corner of your browser; find the part that says tag_ID= followed by a number. Note this number down (e.g., if it says tag_ID=12, your ID is 12).
  2. Access the functions.php File: You can do this in two ways. The easiest method is via the dashboard: go to Appearance > Theme File Editor. On the right-hand side, locate and click on the functions.php file. Alternatively, use an FTP client to navigate to /wp-content/themes/your-theme-name/functions.php.
  3. Insert the Exclusion Code: Scroll to the bottom of the functions.php file and paste the following code snippet. Replace 12 with the actual Category ID you noted in Step 1:
    add_action( 'pre_get_posts', 'exclude_category_from_home' );
    function exclude_category_from_home( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-12' );
        }
    }
    
  4. Save the Changes: If using the Theme File Editor, click the Update File button. If using FTP, save the file and upload it back to the server.
  5. Verify the Result: Visit your homepage and refresh the page. Check that posts from the excluded category are no longer appearing in the main feed.
  6. Check Category Archives: Navigate to the specific category page (e.g., yoursite.com/category/hidden-posts/) to ensure the posts are still visible there and have not been deleted from the site entirely.

Common Mistakes

  • Forgetting the Minus Sign: In the code $query->set( 'cat', '-12' );, the minus sign is critical. Without it, WordPress will do the opposite: it will show only that category and hide everything else.
  • Editing the Parent Theme: If you edit the functions.php file of a parent theme, your changes will be erased the next time the theme updates. Always use a Child Theme to ensure your modifications are permanent.
  • Using the Category Slug: The code requires the numerical ID, not the name or the slug (the URL-friendly version). Using a word like "private" instead of a number will cause the code to fail.
  • Incorrect Hook Placement: Placing the code inside another existing function or before the opening <?php tag will trigger a critical error and potentially crash the site.

Tips

  • Exclude Multiple Categories: If you need to hide more than one category, you can list them separated by commas. For example, to exclude categories 12, 15, and 22, use: $query->set( 'cat', '-12, -15, -22' );.
  • Use a Plugin for Safety: If you are uncomfortable editing code, install a plugin like "Code Snippets." This allows you to add the same logic without touching the core theme files, making it easier to toggle the exclusion on and off.
  • Clear Your Cache: If you apply the code but still see the posts, clear your WordPress caching plugin (like WP Rocket or W3 Total Cache) and your browser cache.

Troubleshooting

If your site displays a "Critical Error" or a white screen after saving the file, you likely have a syntax error (such as a missing semicolon or bracket). To fix this, access your site via FTP, open functions.php, and remove the code you just added. Once the site is back online, double-check that you copied the code exactly as written.

If the posts are still appearing on the homepage, ensure that your homepage is set to "Your latest posts" under Settings > Reading. If you are using a custom-built page as your homepage with a Page Builder (like Elementor or Divi), the pre_get_posts hook may not work because the builder uses its own custom query. In that case, you must use the "Query" or "Exclude" settings within the Page Builder's post widget.

Frequently Asked Questions

Will this hide the category from search results?

No. This method only affects the main query on the homepage. The posts will still be indexed by search engines and will appear in your site's internal search results.

Does this work for tags as well as categories?

The provided code is specifically for categories. To exclude tags, you would need to use $query->set( 'tag__not_in', array( tag_id ) ); instead of the cat parameter.

Can I exclude a category from all archives, not just the homepage?

Yes. To do this, remove the $query->is_home() condition from the if-statement. However, be careful, as this will hide those posts from almost every list on your site.

Is this method better than using a plugin?

Using a code snippet is more lightweight and doesn't add the overhead of an extra plugin. However, plugins are safer for beginners who are not comfortable with PHP.

Will this affect my SEO?

Generally, no. Since the posts are still available via their own URLs and category archives, search engines can still find and index them. It simply changes the organization of your front page.