Mangcoding

icon chat
Mulida Asti - Wednesday, 5 March 2025 - 4 months ago

Creating a Custom Taxonomy Filter in the WordPress Admin

single image
Photo by webfactoryltd on Unsplash

If you have many custom posts on your website, it can sometimes be easier to filter them by 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, which will make it easier for you to filter posts by custom taxonomy.

Here’s what it looks like in the WordPress admin, take a look at the image below!

custom taxonomy mangcoding

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 ) {
        ?>
            <select name="<?php echo $taxonomy_name ?>">
                <option value="">All courses</option>
                <?php
                    foreach ( $courses as $course ) {
                        ?><option value="<?php echo $course->slug ?>"<?php selected( $selected, $course->slug ) ?>><?php echo $course->name ?></option><?php
                    }
                ?>
            </select>
        <?php
    }
}

What you need to remember here:

  • We use the $post_type argument in the function to create a condition so that the filter only appears for specific post types. This argument is only available in WordPress version 4.4 and later, so if you’re using an older version, use the global $typenow instead.
  • If you’re unsure where to insert this code, check out this tutorial.
  • Notice line 26, the string “All courses” can also be dynamically printed, for example: $taxonomy->labels->all_items, but you must first get the taxonomy object with `$taxonomy = get_taxonomy( $taxonomy_name ).

You can also simplify the filter by changing the HTML to wp_dropdown_categories() and extend it to multiple taxonomies.

Here’s the extended version for multiple taxonomies:

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 Mangcoding can share. Hopefully, this article will be useful and provide you with new knowledge. If you have any constructive feedback or suggestions, please comment or send them via Email or Mangcoding’s social media.

Source : Rudrastyh.com

Link Copied to Clipboard