Post Sort by Custom Taxonomy in Admin

PHOTO EMBED

Thu Jul 06 2023 11:45:47 GMT+0000 (Coordinated Universal Time)

Saved by @gshailesh27

add_action('restrict_manage_posts', 'filter_post_type_backend_by_taxonomies', 99, 2);


function filter_post_type_backend_by_taxonomies($post_type, $which) {
    if( 'courses' !== $post_type) {
        return;
    }
   
    $taxonomies = array( 'coursecity' );
   
    foreach( $taxonomies as $taxonomy_slug ) {
        $taxonomy_obj = get_taxonomy( $taxonomy_slug );
        $taxonomy_name = $taxonomy_obj->labels->name;
       
        $terms = get_terms($taxonomy_slug);
       
        echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
        echo '<option value="">' . sprintf( esc_html__( 'Show all %s', 'domain-name' ), $taxonomy_name) . '</option>';
        foreach ( $terms as $term ) {
            printf(
                    '<option value="%1$s" %2$s>%3$s (%4$s)</option>',
                    $term->slug,
                    ( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
                    $term->name,
                    $term->count
            );
            }
        echo '</select>';
    }
}

content_copyCOPY