• Skip to primary navigation
  • Skip to main content
  • Skip to footer

TECHXT

simple extendable solutions

  • Home
  • WordPress Tutorial
  • SEO
  • How to
  • Forum
  • Knowledge Base
    • Domain Name Generator
    • My Tech Projects
    • News
    • Security
  • About
  • Search
Home › Wordpress Tutorial › How to exclude a category from WordPress loop
Wordpress Tutorial

How to exclude a category from WordPress loop

By Sudipto

0

If you looking for a way to exclude categories from your home page then here is a simple tutorial that will help you learn how to exclude one or more categories from your WordPress home page or archive page.

Method 1: Using PHP code

Add the below code to your theme’s functions.php file. Be careful, incorrectly editing your theme’s code can crash your site.

add_action( 'pre_get_posts', 'xt_query_exclude_category' );
function xt_query_exclude_category( $query ) {
    if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )
        $query->set( 'cat', '-5' );
}

The above code excludes category with ID 5 from all loops accept when viewing the website logged with an administrator ID. The trick is done with the line $query->set( 'cat', '-5' ) Note that there is a negative sign before the ID which tells WordPress to exclude that category. You can exclude multiple categories by adding all the category IDs separated by comma. e.g. $query->set( 'cat', '-5,-3,-10' )

Method 2: Using Plugin

WordPress plugin directory has 45,000+ plugins and guess what if you are looking for a WordPress Mod then go and search for a WordPress plugin. There is high probable chance that someone might have already made a plugin for it. If you are someone will little knowledge of programming then I would prefer that you use Ultimate Category Excluder plugin to exclude categories from displaying on home page or archive pages.

Some of the noted features of the Ultimate Category Excluder plugin–

  1. Easy Checkbox style option page to select which categories to exclude.
  2. Ability to exclude different category for different page, namely different settings for front page/home page, archive page, feeds and search result page.

category-excluder

 

I am the founder and developer at TECHXT. Over the decade I have helped hundreds build their own site. All plugins I develop are made have a common goal: Make it as simple as possible and as easy to use as possible.
Sudipto
View all posts by Sudipto
Sudipto's Website / Google+

Do you like this article?

wordpress

About Sudipto

I am the founder and developer at TECHXT. Over the decade I have helped hundreds build their own site. All plugins I develop are made have a common goal: Make it as simple as possible and as easy to use as possible.

Follow me

Footer

Subscribe by email

FOLLOW US :

Tools & Apps

  • Domain Names Generator
  • Our Wordpress Plugins
  • HTML Code Table

TechXT Network

  • www.seecoupons.com – Just another Deals & Coupons site
  • pincodes.techxt.com – A search engine for Indian Pincode.
  • stdcode.techxt.com – A search engine for Indian STD Codes
  • mobileinfo.techxt.com – A search engine for Indian Mobile Codes
  • bankifsccode.techxt.com – A search engine for Indian Bank IFSC Codes.

Info

  • Contact us
  • Privacy Policy

  •   simple extendable solutions

© Copyright 2025 TECHXT.COM

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.