WordPress Admin – Add Dropdown Filters for Custom Taxonomies

Also check out: Using PHP Lambda Functions with WordPress add_action() and add_filter() Functions
There are 2 hooks necessary to 1) create the drop down, and 2) to filter the actual query to show the results.  With 1 taxonomy, it doesn’t really matter, but the more taxonomies you have and want filterable in the WordPress admin, the more copying and pasting the same functions over and over will happen.  Here’s a solution that creates a reusable function where you pass in the $post_type and $taxonomy slug.  To my knowledge, all you need is to be running PHP 5.3 or newer on your server.

WordPress Admin - Filter Custom Taxonomy

$restrict_manage_posts = function($post_type, $taxonomy) {
    return function() use($post_type, $taxonomy) {
        global $typenow;

        if($typenow == $post_type) {
            $selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
            $info_taxonomy = get_taxonomy($taxonomy);

            wp_dropdown_categories(array(
                'show_option_all'   => __("Show All {$info_taxonomy->label}"),
                'taxonomy'          => $taxonomy,
                'name'              => $taxonomy,
                'orderby'           => 'name',
                'selected'          => $selected,
                'show_count'        => TRUE,
                'hide_empty'        => TRUE,
            ));

        }

    };

};

$parse_query = function($post_type, $taxonomy) {

    return function($query) use($post_type, $taxonomy) {
        global $pagenow;

        $q_vars = &$query->query_vars;

        if( $pagenow == 'edit.php'
            && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type
            && isset($q_vars[$taxonomy])
            && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0
        ) {
            $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
            $q_vars[$taxonomy] = $term->slug;
        }

    };

};

Now, what makes this super powerful, is when I want to add the filtering for the taxonomy, I just need these few small lines of code:

add_action('restrict_manage_posts', $restrict_manage_posts('resource', 'resource-type') );
add_filter('parse_query', $parse_query('resource', 'resource-type') );

And if I want to add another filter, it’s just a few small lines again – versus having to copy and paste the same functions over and over, changing just the $post_type and $taxonomy variables.

add_action('restrict_manage_posts', $restrict_manage_posts('resource', 'data-format') );
add_action('parse_query', $parse_query('resource', 'data-format') );

In this case, my post type is Resource, and that was created with the Types WordPress plugin – but it could have also been created with Pods, or another similar plugin, or written directly into your functions.php file.  In the project I am working on, Resource Type and Data Format are custom taxonomies, and those are their slugs that are sent.

Let me know if you use this and find it helpful!