The list_terms_exclusions WordPress PHP Filter allows you to modify the terms that are excluded from a terms query.
Usage
add_filter('list_terms_exclusions', 'your_custom_function', 10, 3); function your_custom_function($exclusions, $args, $taxonomies) { // Your custom code here return $exclusions; }
Parameters
$exclusions
(string) – The “NOT IN” clause of the terms query.$args
(array) – An array of terms query arguments.$taxonomies
(string[]) – An array of taxonomy names.
More information
See WordPress Developer Resources: list_terms_exclusions
Examples
Exclude terms with specific IDs
Exclude terms with the IDs 3, 5, and 7 from the terms query.
add_filter('list_terms_exclusions', 'exclude_specific_terms', 10, 3); function exclude_specific_terms($exclusions, $args, $taxonomies) { $excluded_ids = array(3, 5, 7); $exclusions .= ' AND t.term_id NOT IN (' . implode(',', $excluded_ids) . ')'; return $exclusions; }
Exclude terms with less than 5 posts
Exclude terms that have less than 5 posts associated with them.
add_filter('list_terms_exclusions', 'exclude_terms_with_few_posts', 10, 3); function exclude_terms_with_few_posts($exclusions, $args, $taxonomies) { $exclusions .= ' AND tt.count >= 5'; return $exclusions; }
Exclude terms with a specific slug
Exclude terms with the slug “uncategorized” from the terms query.
add_filter('list_terms_exclusions', 'exclude_terms_with_slug', 10, 3); function exclude_terms_with_slug($exclusions, $args, $taxonomies) { $exclusions .= " AND t.slug != 'uncategorized'"; return $exclusions; }
Exclude terms not assigned to any post
Exclude terms that are not assigned to any post.
add_filter('list_terms_exclusions', 'exclude_unassigned_terms', 10, 3); function exclude_unassigned_terms($exclusions, $args, $taxonomies) { $exclusions .= ' AND tt.count > 0'; return $exclusions; }
Exclude terms from a specific taxonomy
Exclude terms from the “post_tag” taxonomy.
add_filter('list_terms_exclusions', 'exclude_terms_from_taxonomy', 10, 3); function exclude_terms_from_taxonomy($exclusions, $args, $taxonomies) { if (in_array('post_tag', $taxonomies)) { $exclusions .= " AND tt.taxonomy != 'post_tag'"; } return $exclusions; }