Chirpsuite Community

Find answers, ask questions, and connect with our
community around the world.

Home Community Websites & Blogs How to Exclude Category from Blog Page

Tagged: 

  • How to Exclude Category from Blog Page

  • Chirpsuite

    Administrator
    August 8, 2021 at 11:40 pm

    Sometimes, you may want to hide a certain category so it doesn’t appear on the blog page. Since WordPress displays all posts on the blog page by default, here is how you can exclude any category(ies) from the blog page.

    Step One: Note the category ID

    Identify the ID of the category you want to hide. You can do this by navigating to Posts>Categories then hover the mouse over the category you want to hide. You will see the backend url of that category with the category ID. It may look like the url below.

    https://chirpsuite.com/wp-admin/term.php?taxonomy=category&tag_ID=1&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory

    Step Two: Navigate to theme functions.php file

    After noting the category ID, navigate to the active theme’s functions.php file via Appearance>Theme editor then select the active theme from the drop down menu just above the upper right corner of the editor window. After choosing the theme to edit, select functions.php on the right hand panel.

    Step Three: Paste the code in the functions.php file

    Paste the code below into the functions.php file just before the ?> tag. If your theme (or child theme) does not have ?> tag, just scroll to the bottom and paste the code below.

    function exclude_category_posts( $query ) {

    if ( $query->is_home() && $query->is_main_query() ) {

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

    }

    }

    add_action( 'pre_get_posts', 'exclude_category_posts' );

    Step Four: Swap the category ID into the code

    Make sure to insert the category ID you noted in step one into the code above. We have put 1 because we identified our category ID as 1 in step one. Make sure to save changes.

    Exclude multiple categories

    If you want to hide multiple categories from the blog page, identify the category IDs of all the categories you want to hide then add them into the code as below and save changes.

    function exclude_category_posts( $query ) {

    if ( $query->is_home() && $query->is_main_query() ) {

    $query->set( 'cat', '-1, -2, -3, -4 );

    }

    }

    add_action( 'pre_get_posts', 'exclude_category_posts' );

    Happy web developingšŸ˜œ

Viewing 1 of 1 replies

Original Post
0 of 0 posts June 2018
Now
error: