Skip to content

Instantly share code, notes, and snippets.

@ivandoric
Created April 23, 2014 13:40
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 ivandoric/11215573 to your computer and use it in GitHub Desktop.
Save ivandoric/11215573 to your computer and use it in GitHub Desktop.
wordpress: Filter on post types with custom taxonomy in admin
<?php
//Add this to functions PHP
function pippin_add_taxonomy_filters() {
global $typenow;
// an array of all the taxonomyies you want to display. Use the taxonomy name or slug
$taxonomies = array('product-category');
// must set this to the post type you want the filter(s) displayed on
if( $typenow == 'products' ){
foreach ($taxonomies as $tax_slug) {
$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>";
}
}
}
}
add_action( 'restrict_manage_posts', 'pippin_add_taxonomy_filters' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment