I have a custom post type called dogs. Within the Add Dog interface, I have a radio-button group that gives choices for color, which are added to the new dog post as meta data. There are about five color choices.

I also have the color field set up to be displayed as a sortable column in the admin area, when I look at all my dog posts. The sorting works as expected.

So now what I’d like to do is have each color value be a link which I can click, and when I click it, it filters all dog posts by that color value. So if I click “brown,” I’ll see a list of only brown dogs.

On Pages, we can click on an Author’s name and see Pages filtered by that author. This is what I’d like, for color and the CPT dog.

I prefer to just do this in functions.php, and not use an enterprise plugin. Is there a way to do this?

3 Answers
3

Based on @ Andy Macaulay-Brook comment on your question you can try this (it will add a dropdown next to the Filter button on top of the admin listing table):

add_action( 'restrict_manage_posts',  'filter_dogs_by_color' );

// Filter Dogs by Color
function filter_dogs_by_color( $post_type ) {
        if ( 'dog' !== $post_type ) return;    // Replace 'dog' with your post_type

        $taxonomy = 'color'; // Replace 'color' with your taxonomy slug

        $info_taxonomy = get_taxonomy($taxonomy);
        $selected      = isset($_GET[$info_taxonomy->query_var]) ? $_GET[$info_taxonomy->query_var] : '';

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

Leave a Reply

Your email address will not be published. Required fields are marked *