Filter Custom Post Types by Taxonomy in WP Admin Dashboard

Filter CPTs by a linked custom Taxonomy in the WP Admin Dashboard is possible with restrict_manage_posts.

Filter Cpt By Custom Taxonomy

In the screenshot there is a CPT list of posts, with a dropdown menu of a linked custom taxonomy terms which are filterable via the filter button next to it.

The code goes in functions.php

So change to your CPT – example uses ‘light’ and Custom Taxonomy – example uses ‘light_category’.

The action has a late priority of ’99’ so it ends up closer to the filter button in the dashboard.

6 Comments

  1. Brett Atkin on December 8, 2021 at 6:51 pm

    Works perfectly – took 2 minutes to implement. Thanks!!

  2. Sumit Thakur on January 15, 2021 at 6:21 am

    How can I remove defult categories like product cat from product list at side of admin panel. Please refer below image and Please send solution on my email ID

    https://prnt.sc/wnza8h

    Emal ID :: [email protected]

  3. Jake on August 31, 2020 at 8:25 am

    public static function filter_backend_by_taxonomies($post_type, $which)
    {
    // Apply this to a specific CPT
    if (‘product’ !== $post_type) {
    return;
    }

    // A list of custom taxonomy slugs to filter by
    $taxonomies = [ ‘product_cat’ ];

    // loop through the taxonomy filters array
    foreach( $taxonomies as $slug ){
    $taxonomy = get_taxonomy( $slug );
    $selected = ”;
    // if the current page is already filtered, get the selected term slug
    $selected = isset( $_REQUEST[ $slug ] ) ? $_REQUEST[ $slug ] : ”;
    // render a dropdown for this taxonomy’s terms
    wp_dropdown_categories( array(
    ‘show_option_all’ => $taxonomy->labels->all_items,
    ‘taxonomy’ => $slug,
    ‘name’ => $slug,
    ‘orderby’ => ‘name’,
    ‘value_field’ => ‘slug’,
    ‘selected’ => $selected,
    ‘hierarchical’ => false,
    ) );
    }
    }

  4. Fede on December 2, 2019 at 9:50 am

    I have no idea if there is a better way but I made it work by copying the entire code right underneath the first chunk of code but replaced the part that says:

    add_action( ‘restrict_manage_posts’, ‘filter_backend_by_taxonomies’ , 99, 2);

    by

    add_action( ‘restrict_manage_posts’, ‘filter_backend_by_taxonomies_your-post-type’ , 99, 2);

    and also replacing :
    function filter_backend_by_taxonomies_( $post_type, $which )
    by:
    function filter_backend_by_taxonomies_your-post-type( $post_type, $which )

    then replace the other parts where your post type and post type category are.

    So the whole thing would be: (replace all the parts where it says “your-post-type” with yours)

    /**Add custom filters for Custom Post types on admin panel). */
    add_action( ‘restrict_manage_posts’, ‘filter_backend_by_taxonomies_your-post-type’ , 99, 2);
    /* Filter CPT via Custom Taxonomy */
    /* https://generatewp.com/filtering-posts-by-taxonomies-in-the-dashboard/ */

    function filter_backend_by_taxonomies_your-post-type( $post_type, $which ) {
    /*if( array( ‘room’, ‘premises’!== $post_type) );
    return;*/
    // Apply this to a specific CPT
    if (‘your-post-type’ !== $post_type )
    return;

    // A list of custom taxonomy slugs to filter by
    $taxonomies = array( ‘your-post-type_category’) ;

    foreach ( $taxonomies as $taxonomy_slug ) {

    // Retrieve taxonomy data
    $taxonomy_obj = get_taxonomy( $taxonomy_slug );
    $taxonomy_name = $taxonomy_obj->labels->name;

    // Retrieve taxonomy terms
    $terms = get_terms( $taxonomy_slug );

    // Display filter HTML
    echo “”;
    echo ” . sprintf( esc_html__( ‘Show All %s’, ‘text_domain’ ), $taxonomy_name ) . ”;
    foreach ( $terms as $term ) {
    printf(
    ‘%3$s (%4$s)’,
    $term->slug,
    ( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ‘ selected=”selected”‘ : ” ),
    $term->name,
    $term->count
    );
    }
    echo ”;
    }

    }

  5. Max on June 17, 2019 at 3:55 pm

    how do we add another post type to the function, can I just add another post type to the if condition followed by a comma ? such as ” if ( ‘light’, ‘show’ !== $post_type) and adding the taxonomy here as well ( $taxonomies = array( ‘light_category’, ‘show_category’ );

  6. ROSEMARY on May 21, 2019 at 4:05 am

    it doesnt work. We created the custom taxonomies with CPT UI

Leave all Comment