Skip to content

Instantly share code, notes, and snippets.

@hissy
Created October 1, 2013 23:52
Show Gist options
  • Save hissy/6787018 to your computer and use it in GitHub Desktop.
Save hissy/6787018 to your computer and use it in GitHub Desktop.
[WordPress/Filter] Exclude specific category from your blog
<?php
// Exclude 'example' category from your blog
function my_get_terms_args_exclude( $args, $taxonomies ) {
if (!is_admin()) {
$cat = get_category_by_slug( 'example' );
if ( is_object( $cat ) ) {
$args['exclude'] = $cat->term_id;
}
}
return $args;
}
add_filter( 'get_terms_args', 'my_get_terms_args_exclude', 10, 2 );
function my_get_the_terms_exclude( $terms, $post_id, $taxonomy ) {
if (!is_admin() && is_array($terms)) {
$_terms = array();
foreach( $terms as $key => $term ) {
if ($term->slug != 'example' ) {
$_terms[$key] = $term;
}
}
$terms = $_terms;
unset($_terms);
}
return $terms;
}
add_filter( 'get_the_terms', 'my_get_the_terms_exclude', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment