Admin & Dashboard PHP Intermediate

Add Taxonomy Filters to Custom Post Type Lists

Add a dropdown filter for each category-style taxonomy on custom post type list screens, like WordPress has for Posts.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 19 lines
add_action( 'restrict_manage_posts', function ( string $post_type ): void {
	foreach ( get_object_taxonomies( $post_type, 'objects' ) as $taxonomy ) {
		// Posts already have a category filter; skip tag-style taxonomies.
		if ( 'category' === $taxonomy->name || ! $taxonomy->hierarchical || ! $taxonomy->query_var || ! $taxonomy->show_ui ) {
			continue;
		}
		wp_dropdown_categories( [
			'taxonomy'          => $taxonomy->name,
			'name'              => $taxonomy->query_var,
			'value_field'       => 'slug',
			'show_option_none'  => $taxonomy->labels->all_items,
			'option_none_value' => '',
			'selected'          => sanitize_title( wp_unslash( $_GET[ $taxonomy->query_var ] ?? '' ) ),
			'hierarchical'      => true,
			'hide_empty'        => false,
			'show_count'        => true,
		] );
	}
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

WordPress gives Posts a category dropdown, but custom post types like portfolios, events or documents get nothing. This snippet adds a filter for each of their category-style taxonomies automatically.

How it works

On restrict_manage_posts, the snippet looks at every taxonomy attached to the current post type. For hierarchical, visible taxonomies with a query variable, it prints a dropdown of terms using their slugs as values. Because the dropdown is named after the taxonomy’s query variable, WordPress filters the list without any extra code.

The “all” option uses an empty value, so choosing it shows everything instead of searching for a term called “0”.

Things to watch

  • Tag-style (non-hierarchical) taxonomies are skipped to keep the toolbar short. Remove the hierarchical check to include them.
  • WooCommerce already adds its own product category filter; this adds one for any other product taxonomies.

How to test it

Open the list screen for a custom post type with a category-style taxonomy. A dropdown should appear next to the date filter.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only