• Hello I need to remove a single category from posting in the blog but I need that category to stay active. I want to pull a single category into a widget with category id ( which I can do) but I don’t want that category to show up in the main blog. Any help with how to accomplish this would be greatly appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • from: https://codex.www.ads-software.com/Plugin_API/Action_Reference/pre_get_posts

    Exclude categories on your main page
    This is how you can exclude categories of posts from displaying in your blog. For example, if you have 2 categories of posts (uncategorized ‘1’ and another ‘1347’) that you don’t want to display on your ‘home’ blog page, you can use the following in your plugin or theme to omit these categories:

    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-1,-1347' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );
    Moderator keesiemeijer

    (@keesiemeijer)

    Hi dezinezone

    With this in your (child) theme’s functions.php file the category page will be a 404 (page not found).

    function remove_special_category( $query ) {
    	$category_id = 3;
    
    	// not an admin page and is the main query
    	if ( !is_admin() && $query->is_main_query() ) {
    		if ( is_category( $category_id ) ) {
    			$query->set_404();
    		}
    	}
    }
    add_action( 'pre_get_posts', 'remove_special_category' );

    Change the category ID 3 in $category_id = 3; to the category you want to remove from your blog.

    The category can still display in the menu or in post meta though.

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost. Or create a plugin with the code above.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Remove single category from blog (special)’ is closed to new replies.