Privat: Creating a Custom Taxonomy Filter on WordPress Admin DEV
Photo by webfactoryltd on Unsplash
Creating a custom taxonomy filter: If you have many custom posts on your website, it can sometimes be easier to filter them by a specific taxonomy when managing them in your WordPress admin.
In this article, mangcoding will share how to create a custom taxonomy filter in the wordpress. admin, making it easier to filter posts by taxonomy in a custom way.
Here’s what it looks like in the WordPress admin. Please refer to the image below!

After seeing the result above, here is the code that allows you to add a taxonomy filter for a specific taxonomy:
<?php
add_action('restrict_manage_posts', 'rudr_taxonomy_filter');
function rudr_taxonomy_filter($post_type) {
// do nothing it it is not a post type we need
if ('lesson' !== $post_type) {
return;
}
$taxonomy_name = 'course_category';
// get all terms of a specific taxonomy
$courses = get_terms(array(
'taxonomy' => $taxonomy_name,
'hide_empty' => false
));
// selected taxonomy from URL
$selected = isset($_GET[$taxonomy_name]) && $_GET[$taxonomy_name] ? $_GET[$taxonomy_name] : '';
if ($courses) {
echo '<select name="' . $taxonomy_name . '">';
echo '<option value="">All courses</option>';
foreach ($courses as $course) {
echo '<option value="' . $course->slug . '"' . selected($selected, $course->slug) . '>' . $course->name . '</option>';
}
echo '</select>';
}
}
What you need to keep in mind here:
- We use the $post_type argument in the function to create a condition so that the filter will only be displayed for a specific post type. However, this argument is only available from WordPress version 4.4, so if you’re using an older version of WP, you can just use the global $typenow variable instead.
- If you’re unsure where to place the code, please read this tutorial.
- Pay attention to line 26, the string “All courses” can also be dynamically printed, for example: $taxonomy->labels->all_items, but of course, you need to retrieve the taxonomy object first with $taxonomy = get_taxonomy($taxonomy_name).
It is also possible to simplify the filter by changing the HTML to wp_dropdown_categories() and extending it to multiple taxonomies.
<?php
add_action('restrict_manage_posts', 'rudr_taxonomy_filter');
function rudr_taxonomy_filter($post_type) {
// let's decide about post type first
if ('my_post_type' !== $post_type) {
return;
}
// pass multiple taxonomies as an array of their slugs
$taxonomies = array('taxonomy_1', 'taxonomy_2');
// for every taxonomy we are going to do the same
foreach ($taxonomies as $taxonomy) {
$taxonomy_object = get_taxonomy($taxonomy);
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
wp_dropdown_categories(array(
'show_option_all' => $taxonomy_object->labels->all_items,
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name', // slug / count / term_order etc
'value_field' => 'slug',
'selected' => $selected,
'hierarchical' => true,
));
}
}
That’s the article that Mangcoding can share. Hopefully, this article will be useful and provide new knowledge for you. If you have any feedback or suggestions, feel free to comment or send them via Email and Mangcoding’s social media.
Source: Rudrastyh.com