Filter Posts by Custom Taxonomy in Admin

A post on Pippin Williamson’s ‘Pippins Plugins’ site shows how to add a drop down to the WordPress admin to enable a custom post type list to be filtered by a custom taxonomy:

https://pippinsplugins.com/post-list-filters-for-custom-taxonomies-in-manage-posts

Here is how I used the function, with a fix for a possible isset notice suggested by Devin Price, in a plugin which includes taxonomies for ‘Role’ and ‘Location’ to display the posts in a ‘Team Members’ Custom Post Type.

function cc_team_add_taxonomy_filters() {
	global $typenow;
	// an array of all the taxonomies you want to display. Use the taxonomy name or slug
	$taxonomies = array('role','location');
	// must set this to the post type you want the filter(s) displayed on
	if( $typenow == 'team_members' ){
		foreach ($taxonomies as $tax_slug) {
			$current_tax_slug = isset( $_GET[$tax_slug] ) ? $_GET[$tax_slug] : false;
			$tax_obj = get_taxonomy($tax_slug);
			$tax_name = $tax_obj->labels->name;
			$terms = get_terms($tax_slug);
			if(count($terms) > 0) {
				echo "";
			}
		}
	}
}
add_action( 'restrict_manage_posts', 'cc_team_add_taxonomy_filters' );