Skip to content

Instantly share code, notes, and snippets.

@yanknudtskov
Last active February 9, 2019 20:51
Show Gist options
  • Save yanknudtskov/9b92613c8be4d8c0938b10f00f2141d7 to your computer and use it in GitHub Desktop.
Save yanknudtskov/9b92613c8be4d8c0938b10f00f2141d7 to your computer and use it in GitHub Desktop.
Adding a taxonomy filter for post types in Admin Screens #woocommerce #wordpress #wp-admin
<?php
function yanco_add_taxonomy_filters() {
global $typenow;
// an array of all the taxonomyies you want to display. Use the taxonomy name or slug
$taxonomies = array('alle-brands');
// must set this to the post type you want the filter(s) displayed on
if( $typenow == 'product' ){
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=''>Alle $tax_name</option>";
foreach ($terms as $term) {
if( isset( $_GET[$tax_slug] ) ) {
echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
} else {
echo '<option value="">' . $term->name .' (' . $term->count .')</option>';
}
}
echo "</select>";
}
}
}
}
add_action( 'restrict_manage_posts', 'yanco_add_taxonomy_filters' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment