Skip to content

Instantly share code, notes, and snippets.

@ngearing
Last active June 8, 2018 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngearing/0674f1a0e25e54f15e5f676b08bdb5b4 to your computer and use it in GitHub Desktop.
Save ngearing/0674f1a0e25e54f15e5f676b08bdb5b4 to your computer and use it in GitHub Desktop.
Exclude terms in term lists from non Admins in WordPress
<?php
/**
* Exclude some terms from the term lists.
*
* @param WP_Term_Query $term_query The class instance instance.
* @return void
*/
function exclude_terms( $term_query ) {
if (
is_feed() ||
current_user_can( 'edit_users' )
) {
return;
}
$taxonomy = 'tribe_events_cat'; // taxonomy.
$terms_to_exclude = [ '277' ]; // term ids.
if ( in_array( $taxonomy, $term_query->query_vars['taxonomy'] ) ) {
$term_excludes = $term_query->query_vars['exclude'];
$term_excludes = array_merge( is_array( $term_excludes ) ? $term_excludes : [], $terms_to_exclude );
$term_query->query_vars['exclude'] = $term_excludes;
}
}
add_action( 'pre_get_terms', 'exclude_terms' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment