You’ll find an appropriate filter in the get_terms
function which is called by get_categories
.
See source code https://github.com/WordPress/WordPress/blob/master/wp-includes/taxonomy.php#L1247
We don’t use any of the callback arguments in this case. We can use array_filter
on the list of terms (WP_Term) objects. See https://developer.wordpress.org/reference/classes/wp_term/
This allows us to remove a term with a particular ID
add_filter( 'get_terms', function( $terms, $taxonomies, $args, $term_query ){
return array_filter($terms, function( $term ) {
return $term->term_id !== 2;
});
}, 10, 4 );
Note that get_categories
by default only returns categories which have posts assigned, you can change that by adding hide_empty => false
to the $args
array.