Home » BLOG » WordPress » Adding Tags filter to Posts in WordPress admin area

Adding Tags filter to Posts in WordPress admin area

category:  WordPress

Hi there. Today I have time so I would like to share how to add tags filter to Posts in the admin area. You may notice it on the Posts page in the admin area, you see the category filter there but no tags filter yet. This post will show you how.

tags filter posts
Posts page without Tags filter

We will use the “restrict_manage_posts” action hook and add the code into functions.php at your active theme.

Below is the code.

 * Add Tags filter on admin area for Posts
function ar_posts_add_taxonomy_filters() {
	global $typenow;

	// An array of all the taxonomyies you want to display. Use the taxonomy name or slug
	$taxonomies = array('post_tag');  

	// must set this to the post type you want the filter(s) displayed on
	if ( $typenow == 'post' ) {

		foreach ( $taxonomies as $tax_slug ) {
		        // note: the GET variale you want, is 'tag'. simply check at URL parameter.
			$current_tax_slug = isset( $_GET['tag'] ) ? $_GET['tag'] : false;
			$tax_obj = get_taxonomy( $tax_slug );
			$tax_name = $tax_obj->labels->name;
			$terms = get_terms($tax_slug);
			if ( count( $terms ) > 0) {
			        // The select name must be 'tag' ONLY.
				echo "<select name='tag' id='$tax_slug' class='postform'>";
				echo "<option value=''>$tax_name</option>";
				foreach ( $terms as $term ) {
					echo '<option value=' . $term->slug, $current_tax_slug == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
				echo "</select>";

add_action( 'restrict_manage_posts', 'ar_posts_add_taxonomy_filters' );

Credit code from GitHub link.

Once you save the changes and refresh your admin area, you will see the tags filter next to the category filter on the Posts page as shown below.

tags filter posts 2

Hopefully, this post is useful for you.