WordPress Snippets at WPcustoms

Admin filter for custom taxonomies

Add a filtering dropdown box to your custom post type table with this function. Use the array for the taxonomies you want to list and specify the custom post type (here: movies) where the filter dropdown box will show.


/**
 * Snippet Name: Admin filter for custom taxonomies
 * Snippet URL: https://wpcustoms.net/snippets/admin-filter-for-custom-taxonomies/
 */
  // add filtering option
function wpc_add_taxonomy_filters() {
    global $typenow;

    // an array of all the taxonomies you want to display. Use the taxonomy name or slug - each item gets its own select box.
    $taxonomies = array('genre', 'director');

    // use the custom post type here
    if( $typenow == 'movies' ){

        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 "";
            }
        }
    }
}
add_action( 'restrict_manage_posts', 'wpc_add_taxonomy_filters' );