Skip to content
Prem Tiwari
  • WP Tutorials
  • Plugins
  • About Me
  • Featured On
  • Let’s Talk
Prem Tiwari
Home / WordPress Tutorials / How to Exclude a Category From WordPress Loops

How to Exclude a Category From WordPress Loops

ByPrem Tiwari Last updated onJune 19, 2024

In this post, you will learn how to Exclude a Category From WordPress Loops. Use the below code snippets inside your theme’s functions.php file to exclude a category from WordPress loop.

/**
 * Exclude a category from all WordPress loops
 */

add_action( 'pre_get_posts', function( $query ) { // anonymous callback
    
    global $wp_query; 

    // Hard coded category ID, but can be dynamic:     esc_attr(get_option('your-cat-id')); 
    $excluded_cat_id = 25;

    // add category ID to existing, avoid overwritting it 
    $cat[] = $query->get( 'cat' );
    $cat[] = "-" . $excluded_cat_id;

    $query->set( 'cat', $cat );
    }
});

Found it helpful? Share it with your friends on social media!

  • Facebook
  • Post on X
  • LinkedIn
Prem Tiwari

Prem Tiwari is a passionate advocate of open source technology, with over 10+ years of experience in the WordPress domain. He has great experience when it comes to scaled WordPress projects which caters the need for big enterprise clients on WordPress and WordPress VIP-GO platform.

Facebook Twitter Instagram YouTube Linkedin

Post navigation

Previous Previous
How to Fix: Image size smaller than recommended size
NextContinue
How to install WP-CLI to manage WordPress site

Collections

  • Crawling and indexing
  • Meetup
  • PHP
  • Plugins
  • SEO
  • Tech Talks
  • WooCommerce Tutorials
  • WordCamp
  • WordPress Tutorials
  • Privacy
  • Cookies
  • Disable Update Notifications
Facebook Twitter Instagram YouTube Linkedin WordPress
Scroll to top
  • WP Tutorials
  • Plugins
  • About Me
  • Featured On
  • Let’s Talk