search ]

How to Remove a Category from the Main Loop via functions.php

To remove a category from the main loop in WordPress, you need to modify the functions.php file of your theme. Here’s a step-by-step guide to accomplish this:

Locate the functions.php File

  • Go to your WordPress dashboard.
  • Navigate to Appearance > Theme Editor
  • Select your theme and open the functions.php file.

This can obviously be done as well by editing the file using ftp client.

Add a Function to Exclude the Category

You need to use the pre_get_posts action hook to modify the main query before it is executed. Here’s the code to add to your functions.php file:

function exclude_category_from_main_loop($query) {
    // Ensure this code runs only on the main query in the frontend
    if (!is_admin() && $query->is_main_query()) {
        // Exclude category with ID 123 (replace with your category ID)
        $query->set('cat', '-123');
    }
}
add_action('pre_get_posts', 'exclude_category_from_main_loop');

In this example, replace 123 with the ID of the category you want to exclude. The cat parameter in the set method accepts a negative value to exclude the specified category.

Save the Changes

After adding the code, save the changes to the functions.php file.

Verify the Changes

Visit your WordPress site to ensure the category is excluded from the main loop.

Additional Notes

Finding the Category ID

To find the category ID, go to Posts > Categories in your WordPress dashboard. Hover over the category name, and you will see the ID in the URL at the bottom of your browser.

Excluding Multiple Categories

If you need to exclude multiple categories, you can do so by separating their IDs with commas, like this:

function exclude_category_from_main_loop($query) {
    // Ensure this code runs only on the main query in the frontend
    if (!is_admin() && $query->is_main_query()) {
        // Exclude multiple categories by IDs (replace with your category IDs)
        $query->set('cat', '-123,-456,-789');
    }
}
add_action('pre_get_posts', 'exclude_category_from_main_loop');

Conditional Exclusions

If you need to exclude categories conditionally (e.g., only on the home page), you can add additional checks within the if statement:

function exclude_category_from_main_loop($query) {
    // Ensure this code runs only on the main query in the frontend
    if (!is_admin() && $query->is_main_query() && is_home()) {
        // Exclude category with ID 123 (replace with your category ID)
        $query->set('cat', '-123');
    }
}
add_action('pre_get_posts', 'exclude_category_from_main_loop');

By following these steps, you can effectively remove a category from the main loop in WordPress using the functions.php file.

Roee Yossef
Roee Yossef

I develop pixel-perfect custom WordPress themes, delivering high-performance, SEO-optimized websites. Have a project in mind or need assistance? Feel free to contact me!

0 Comments...

Leave a Comment

Add code using the buttons below. For example, to add PHP click the PHP button & add the code inside the shortcode. Typo? Please let us know...

Recently Updated