Skip to content

Instantly share code, notes, and snippets.

@mattboon
Created November 14, 2012 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattboon/4072627 to your computer and use it in GitHub Desktop.
Save mattboon/4072627 to your computer and use it in GitHub Desktop.
WordPress - Add custom taxonomy filters to custom post types
<?php
// Add category filters to WP custom post types
add_action( 'restrict_manage_posts', 'my_add_category_filters' );
function my_add_category_filters() {
global $typenow;
if( $typenow == "event" ){
$taxonomy = 'event_type';
}
elseif( $typenow == "guideline" ){
$taxonomy = 'guideline_type';
}
elseif( $typenow == "resource" ){
$taxonomy = 'resource_type';
}
if( $typenow != "post" && $typenow != "page" ) {
$filters = array($taxonomy);
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=''>View all categories</option>";
foreach ($terms as $term) { echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>'; }
echo "</select>";
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment