hide specific categories from showing

I haven’t tested this, so it might need to be tweaked (specifically, I’m not sure if I got the names of the params right on the term object), but this should get you most of the way there.

//add filter
add_filter( 'get_the_terms', 'my_exclude_terms', 10, 1 );
//function to do filtering
function my_exclude_terms( $terms ) {
    //only filter for the homepage
    if( is_home() ) { // you may want to use is_front_page(), depending on your settings
        //list the unwanted terms
        $unwanted_terms = array( // fill this array with the unwanted term ids, slugs, or names
            14,
            18
        );
        // loop through the terms
        foreach( $terms as $k => $term ) {
            //only remove term if it's ID or slug is in the array.
            if(
              in_array( $term->term_id, $unwanted_terms, true ) || //comment out this line to remove term ID checking
              in_array( $term->slug, $unwanted_terms, true ) ||    //comment out this line to remove slug checking
              in_array( $term->name, $unwanted_terms, true )       //comment out this line to remove name checking
            ) {
                unset( $terms[$k] );
            }
        }
    }
    return $terms;
}

I used the get_the_terms filter because it’s the last filter before the terms are turned to HTML, which makes it significantly more difficult to parse. If you’re a complete novice with PHP and need help troubleshooting or implementing, post a comment. Good luck!

Leave a Comment