Simple Posts Filter by Custom Taxonomy in WP Admin Without Plugins

Okay, what if you have a lot of posts (or maybe custom post type posts) on your blog? How do you manage them in your admin panel? You use search for that, don’t you? Let me show you a very simple code example, which will add on your posts page a filter by taxonomy terms […]

/17 comments

Okay, what if you have a lot of posts (or maybe custom post type posts) on your blog? How do you manage them in your admin panel? You use search for that, don’t you?

Let me show you a very simple code example, which will add on your posts page a filter by taxonomy terms (like category filter).

term taxonomy posts filter in wordpress admin

And that is the code for your functions.php file:

function rudr_posts_taxonomy_filter() {
	global $typenow; // this variable stores the current custom post type
	if( $typenow == 'post' ){ // choose one or more post types to apply taxonomy filter for them if( in_array( $typenow  array('post','games') )
		$taxonomy_names = array('platform', 'device');
		foreach ($taxonomy_names as $single_taxonomy) {
			$current_taxonomy = isset( $_GET[$single_taxonomy] ) ? $_GET[$single_taxonomy] : '';
			$taxonomy_object = get_taxonomy( $single_taxonomy );
			$taxonomy_name = strtolower( $taxonomy_object->labels->name );
			$taxonomy_terms = get_terms( $single_taxonomy );
			if(count($taxonomy_terms) > 0) {
				echo "<select name='$single_taxonomy' id='$single_taxonomy' class='postform'>";
				echo "<option value=''>All $taxonomy_name</option>";
				foreach ($taxonomy_terms as $single_term) {
					echo '<option value='. $single_term->slug, $current_taxonomy == $single_term->slug ? ' selected="selected"' : '','>' . $single_term->name .' (' . $single_term->count .')</option>'; 
				}
				echo "</select>";
			}
		}
	}
}
 
add_action( 'restrict_manage_posts', 'rudr_posts_taxonomy_filter' );

Related Posts

Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WordPress?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Comments — 17

Leave a comment

php js HTML CSS Code