WP admin: Custom taxonomy filter in custom post type

January 26, 2012 by fMajakovskij

If you are hardly try to find (like me) a simple solution to add a dropdown filter in custom post type admin list, here it is a snippet slightly modified from several sources around there.

Not yet perfect but at least it works mostly.

Put this in your functions.php file and have fun!

The magic happen thanks to the get_object_taxonomies function:


add_action( 'restrict_manage_posts', 'my_restrict_manage_posts' );
function my_restrict_manage_posts() {
global $typenow;
$taxonomy = $typenow.'_type';
if($typenow != "post" )
{
$filters = get_object_taxonomies($typenow);
foreach ($filters as $tax_slug) {
$tax_obj = get_taxonomy($tax_slug);
$tax_name = $tax_obj->labels->name;
$terms = get_terms($tax_slug);
echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
echo "<option value=''>Show All $tax_name</option>";
foreach ($terms as $term) { echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>'; }
echo "</select>";
}
}
}
Tags:

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>