Last updated on September 23, 2023

Exclude a Category from the Main Blog Loop

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Exclude categories from blog loop.

To exclude a category from the main blog loop in WordPress, you can use the pre_get_posts action hook along with the is_main_query() function to target the main query on the blog page. Within the callback function, you can modify the query parameters to exclude the desired category.

Here’s an example code snippet that demonstrates how to exclude a category from the main blog loop:

function wpsnippets_exclude_category_from_main_loop( $query ) {
    if ( $query->is_main_query() && ! is_admin() ) {
        $query->set( 'cat', '-5' ); // Replace 5 with the ID of the category you want to exclude
    }
}
add_action( 'pre_get_posts', 'wpsnippets_exclude_category_from_main_loop' );

In this example, we’re using the pre_get_posts action hook to modify the main query before it is executed. The is_main_query() function ensures that we only target the main query on the blog page and not any other custom queries or admin pages. The ! is_admin() condition ensures that the exclusion doesn’t apply in the WordPress admin area.

Inside the callback function, we use the set() method of the $query object to modify the cat parameter of the query. By prefixing the category ID with a minus sign (-), we exclude that category from the query results. You should replace 5 with the actual ID of the category you want to exclude.

By adding this code to your theme’s functions.php file or a custom plugin, the specified category will be excluded from the main blog loop, and its posts will not be displayed on the blog page.

This code snippet can be useful when you want to exclude a specific category from being displayed in the main blog loop. For example, if you have a “Featured” category that you don’t want to appear in the main blog feed, you can use this code to exclude it.

Examples

Example 1: Exclude a Category from the Main Blog Loop

This use case demonstrates how to exclude a specific category from the main blog loop in WordPress. The code example below uses the pre_get_posts action hook to modify the main query and exclude a category with the ID of 5.

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

Explanation: The pre_get_posts action hook is used to modify the main query before it is executed. In this example, we check if the query is the main query and not in the admin area. If both conditions are met, we use the set() method of the query object to exclude the category with the ID of 5 by prefixing it with a minus sign.

Example 2: Exclude Multiple Categories from the Main Blog Loop

This use case demonstrates how to exclude multiple categories from the main blog loop in WordPress. The code example below extends the previous example and excludes categories with IDs 5, 8, and 12.

function wpsnippets_exclude_categories( $query ) {
    if ( $query->is_main_query() && ! is_admin() ) {
        $query->set( 'category__not_in', array( 5, 8, 12 ) );
    }
}
add_action( 'pre_get_posts', 'wpsnippets_exclude_categories' );

Explanation: Similar to the previous example, we use the pre_get_posts action hook to modify the main query. This time, we use the category__not_in parameter in the set() method to exclude multiple categories. The category IDs are passed as an array.

Example 3: Exclude a Category by Slug from the Main Blog Loop

This use case demonstrates how to exclude a category by its slug from the main blog loop in WordPress. The code example below modifies the previous example and excludes a category with the slug “uncategorized”.

function wpsnippets_exclude_category_by_slug( $query ) {
    if ( $query->is_main_query() && ! is_admin() ) {
        $category = get_category_by_slug( 'uncategorized' );
        if ( $category ) {
            $query->set( 'cat', '-' . $category->term_id );
        }
    }
}
add_action( 'pre_get_posts', 'wpsnippets_exclude_category_by_slug' );

Explanation: In this example, we use the get_category_by_slug() function to retrieve the category object based on its slug. If the category exists, we exclude it from the main query by using its term ID with a minus sign. The rest of the code is similar to the previous examples.

Last updated on September 23, 2023. Originally posted on September 24, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *