Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Exclude a Category From WordPress Loops

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 );
    }
});

The post How to Exclude a Category From Wordpress Loops appeared first on Prem Tiwari.



This post first appeared on Programming Blog Focused On Web Technologies, please read the originial post: here

Share the post

How to Exclude a Category From WordPress Loops

×

Subscribe to Programming Blog Focused On Web Technologies

Get updates delivered right to your inbox!

Thank you for your subscription

×