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.

3 Comments

  1. 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 ”;
    }

    }

  2. 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’ );

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

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

Leave a Comment