Filter Custom Posts by Custom Taxonomy in WordPress Admin

Filter Custom Posts by Custom Taxonomy in WordPress Admin

For normal posts types, wordpress come with the option to filter by category drop down.

But when you create a custom post, wordpress by default doesn’t support the category filter at the point of writing.

But this can be achieve with few lines of code. All you need to do is copy paste the code I am sharing below in the functions.php template of your theme. And it works its magic.

I had used this code fair few times during my custom wordpress development projects.

function wpguru_cp_filter() {
	global $typenow;

	// select the custom taxonomy
	$taxonomies = array('porftolio_category');

	// select the type of custom post
	if( $typenow == 'porftolio' ){

		foreach ($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'>";
				echo "<option value=''>Show All $tax_name</option>";
				foreach ($terms as $term) {
					echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
				}
				echo "</select>";
			}
		}
	}
}
add_action( 'restrict_manage_posts', 'wpguru_cp_filter' );

Use case:
You want to show 5 Featured Portfolio at homepage, but you have hundereds of portfolio. Easy way to do so filter out the Featured Portfolio and get rid of portfolio you don’t want to show and add the new one.

PS: This code had been courtesy of Pippin Plugins.


About Author

I am Robin - WordPress Developer based in Sydney. I work as Digital Developer. I do some projects now and then, and love blogging about WordPress and Web Design.