I was trying to find a way to filter my custom post types by category in the admin view and was having difficulty learning how to do it. After a ton of searching I found a great snippet of code to achieve this. The only drawback is you cannot use it as an array and must copy and paste the code each time you want to use it in a post type.
__("Show All {$info_taxonomy->label}"),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => true,
));
};
}
/**
* Filter posts by taxonomy in admin
* @author Mike Hemberger
* @link http://thestizmedia.com/custom-post-type-filter-admin-custom-taxonomy/
*/
add_filter('parse_query', 'tsm_convert_id_to_term_in_query');
function tsm_convert_id_to_term_in_query($query) {
global $pagenow;
$post_type = 'team'; // change to your post type
$taxonomy = 'group'; // change to your taxonomy
$q_vars = &$query->query_vars;
if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
$term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
$q_vars[$taxonomy] = $term->slug;
}
}
To get this to work you need to paste it into your functions.php file and add your post type where it says “team” and your taxonomy for that post type where it says “group”. If you want to use this for multiple post types you need to make sure you change the function names/ids each time you copy and paste this. Full credit for this snippet goes to http://thestizmedia.com/custom-post-type-filter-admin-custom-taxonomy/