Adding Custom Taxonomies to all posts page

2019-08-25 07:57发布

I've got a custom post type, Staff, and this post type has a custom taxonomy, Roles. I would like to be able to sort by/see roles on all staff page in the wp-admin backend like how categories and tags work by default for posts.

Thanks

screenshot

Custom post type

function register_staff(){
    $labels = array(
        'name' => 'Staff',
        'singular_name' => 'Staff',
        'add_new' => 'Add Staff',
        'all_items' => 'All Staff',
        'add_new_item' => 'Add Staff Member',
        'edit_item' => 'Edit Staff Member',
        'new_item' => 'New Staff Member',
        'view_item' => 'View staff',
        'search_item' => 'Search staff',
        'not_found' => 'No Items Found',
        'not_found_in_trash' => 'No staff found in trash',
    );
    $args = array(
        'labels' => $labels,
        'public' => false,
    'show_ui' => true,
        'has_index' => true,
        'has_archive' => true,
        'publicly_queryable' => true,
        'query_var' => true,
        'has_archive' => true,
    'rewrite' => true,
        'capability_type' => "post",
        'hierarchical' => false,
        'supports' => array(
            'title',
            'editor',
            'page-attributes',
            'excerpt',
            'thumbnail',
            'revisions',
        ),
        'taxonomies' => array("role"),
        'menu_position' => 5,
        'menu_icon' => "dashicons-businessperson",
        'exclude_from_search' => false,
    );
    register_post_type('staff', $args);
}

add_action( "init", "register_staff");

Custom Taxonomy

add_action( "init", "register_staff");

function build_taxonomies() {
  register_taxonomy('role', 'staff', array(
    'label' => 'Roles',
    'public' => true,
  ));
}
add_action( 'init', 'build_taxonomies', 0 );

1条回答
【Aperson】
2楼-- · 2019-08-25 08:39

See https://codex.wordpress.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column

Untested like this:

if ( !function_exists('AddTaxColumn') ) { 

    function AddTaxColumn($cols) { 

    $cols['yourtaxonomy'] = __('My Taxonomy'); 
    return $cols; 

    } 

    function AddTaxValue($column_name, $post_id) { 

        if ( 'yourtaxonomy' == $column_name ) { 

            $tax_id = wp_get_post_terms( $post_id, 'yourtaxonomy' );


            if ($tax_id) {

                $taxonomies = join( ", ", $tax_id );

                echo $taxonomies; 

            }
        }
    }

add_filter( 'manage_team_posts_columns', 'AddTaxColumn' ); 
add_action( 'manage_team_posts_custom_column', 'AddTaxValue', 10, 2 ); 
add_filter( 'manage_edit-team_sortable_columns', 'AddTaxColumn' );
}
查看更多
登录 后发表回答