Skip to content

Instantly share code, notes, and snippets.

@WebEndevSnippets
Created January 8, 2013 19:57
Show Gist options
  • Save WebEndevSnippets/4487342 to your computer and use it in GitHub Desktop.
Save WebEndevSnippets/4487342 to your computer and use it in GitHub Desktop.
WordPress: Add Taxonomy Filter to CPT Admin
add_action( 'restrict_manage_posts', 'we_add_submissions_taxonomy_filters' );
/**
* Add taxonomy filter to the Submissions CPT admin page
*
*/
function we_add_submissions_taxonomy_filters() {
global $typenow;
// an array of all the taxonomies you want to display. Use the taxonomy name or slug
$taxonomies = array('we_type');
// must set this to the post type you want the filter(s) displayed on
if( $typenow == 'we_submission' ) {
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 "<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>";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment