Yes, you can use the wp_get_object_terms
filter to remove the hidden category from any list that uses default WordPress methods like wp_get_post_categories()
.
add_filter('wp_get_object_terms', 'wpse_remove_unwanted_category', 10, 4);
function wpse_remove_unwanted_category($terms, $object_ids, $taxonomies, $args) {
// we're only interested in category, if this is another taxonomy
if ($args['taxonomy'] !== 'category') {
// return early
return $terms;
}
// 34 and 1509 are ids of categories we want to remove
$remove_categories = array(34, 1509);
$filtered_terms = array_filter($terms, function($term) use ($remove_categories) {
// if term_id is in the array, filter it out (aka remove from array)
return ( ! in_array($term->term_id, $remove_categories));
});
return $filtered_terms;
}
However, this filter will remove the unwanted categories from many lists, not only those in the frontend.