Snippets: Exclude Categories from Showing on The Front-End

This snippet will allow you to remove categories from showing up on the front-end in sections such as the post entry and singular post meta where it displays a list of the categories a post belongs to. This won't hide any posts from your site, just remove the link to the category.

add_filter( 'wp_get_object_terms', function( $terms, $object_ids, $taxonomies, $args ) {

    // Don't mess with admin stuff
    if ( is_admin() ) {
        return $terms;
    }

    // Category slugs to exclude
    $exclude = array( 'uncategorized' );

    // Loop through terms and remove items
    if ( $terms ) {
        foreach( $terms as $key => $term ) {
            if ( is_object( $term )
                && $term->taxonomy == 'category'
                && in_array( $term->slug, $exclude )
            ) {
                unset( $terms[$key] );
            }
        }
    }

    // Return terms
    return $terms;

}, PHP_INT_MAX, 4 );

Snippet Type: PHP

All PHP snippets should be added via a child theme's functions.php file or via the Code Snippets Plugin (or alternative plugin)