Hack: Exclude Categories or Tags From Lists

Categories and tags are useful for a number of purposes. Sometimes you want to have a category or tag that is for backend purposes, and not necessarily for display. Sometimes you want writers on your site to only see certain terms. You can filter these easily with the list_terms_exclusions hook.

To start with, here is some simple code that will filter out two terms for anyone viewing the site. But it doesn’t filter those terms if you are using the admin to write or edit posts.

add_filter('list_terms_exclusions', 'wpfilter_list_terms_exclusions');
function wpfilter_list_terms_exclusions($exclusions) {
	if (!is_admin()) {
		$exclusions .= " AND t.slug NOT IN ('term1', 'term2')";
	}
	return $exclusions;
}

The idea here is you can have categories and terms assigned to your posts that readers never see in widgets and lists. But you can see them and assign them in the admin. I can see this as being useful when you want to narrow reader-viewable category lists.

You can add more terms easily, both categories and tags. You can also adjust the if condition to meet other needs. For instance, you can use current_user_can for instance to hide categories from writers so only admins can assign a post to a category.

Leave a Reply

Your email address will not be published. Required fields are marked *