Categories
Tips & Tricks

Adding Tags Filter to the Posts List

By default, WordPress does not ship the tag filter capability to its posts list whilst, in many cases, we need it to quickly access to the content type we have classified.

By default, WordPress does not ship the tag filter capability to its posts list whilst, in many cases, we need it to quickly access to the content type we have classified.

To do so, add the following codes:

<?php

/***********************************
* Function to add filter by tags
* in the posts list
*/

function kc_add_taxonomy_filters() {

    global $typenow;

    // an array of all the taxonomyies you want to display. Use the taxonomy name or slug

    $my_taxonomies = array( 'post_tag' );

    switch ( $typenow ) {

        case 'post':

            foreach ( $my_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 alignleft actions'>";

                    echo "<option value=''>Show All $tax_name</option>";

                    foreach ( $terms as $term ) {

                        echo '<option value="', $term->slug,'" ',selected( @$_GET[$tax_slug] == $term->slug , $current = true, $echo = false ) , '>' , $term->name ,' (' , $term->count ,')</option>';

                    }

                    echo "</select>";

                }

           }

        break;

    }

}

add_action( 'restrict_manage_posts', 'kc_add_taxonomy_filters' );

To your functions.php then you’ll have this filter option right in your posts list:

Add Tag Filter in Posts List

By Vannkorn

I am a WordPress lover. I make my living using WordPress.

Leave a Reply

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