Adding custom taxonomy filters to admin for the custom post type
Home » BLOG » WordPress » Adding custom taxonomy filters to admin for the custom post type

Adding custom taxonomy filters to admin for the custom post type

category:  WordPress

Continuing from my previous post, today I will share how to add any custom taxonomy filters to the admin area for the custom post type.

Custom Post Type

In my case, I have already created my portfolio custom post type. You can follow “How to create the custom post type” link to create your own.

My portfolio post type has the portfolio type ( portfolio-type as a slug ) and portfolio tag ( portfolio-tag as a slug ). So when I view my portfolio items on the admin area, I will see those columns on the Portfolio page as shown below.

As you can see, there are no Portfolio Type and Portfolio Tags filters.

Add Custom Taxonomy Filters

In order to add the custom taxonomy filters, we will use the “restrict_manage_posts” action hook and add the code into functions.php at your active theme.

Below is the code.

function ar_portfolio_add_taxonomy_filters() {
	global $typenow;

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

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

		foreach ( $taxonomies as $tax_slug ) {
			$current_tax_slug = isset( $_GET[$tax_slug] ) ? $_GET[$tax_slug] : false;
			$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'>";
				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_portfolio_add_taxonomy_filters' );

Once the code is saved and you refresh the admin area, you will see the portfolio type and portfolio tags filters as shown below.

Go ahead, test those filters. It should work fine.

How to use the code with your own custom post type and custom taxonomies

You can use it with other custom post types as well.

For example, you create a movie post type. Then you create a movie type (movie-type as a slug) and movie tags (movie-tag as a slug). You will use the code as shown below.

function ar_movie_add_taxonomy_filters() {
	global $typenow;

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

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

		foreach ( $taxonomies as $tax_slug ) {
			$current_tax_slug = isset( $_GET[$tax_slug] ) ? $_GET[$tax_slug] : false;
			$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'>";
				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_movie_add_taxonomy_filters' );

Credit code from Github link.

Note that, you must be sure the slug for custom post types and taxonomies are unique. Meaning, none of your theme and plugins use them yet.