あのときコード

管理画面記事一覧でタクソノミーの絞り込みをできるようにする

管理画面 記事一覧 タクソノミー 絞り込み select

function custom_post_restrict_manage_posts() {

    if ( 'custom_post' != get_current_screen()->post_type ) return;

    $taxonomy = 'custom_post_category';

    wp_dropdown_categories( array(
        'show_option_all' => 'すべて',
        'selected' => get_query_var( $taxonomy ),
        'hide_empty' => 0,
        'name' => $taxonomy,
        'taxonomy' => $taxonomy,
        'value_field' => 'slug',
    ) );
}
add_action('restrict_manage_posts', 'custom_post_restrict_manage_posts');

一覧にタクソノミー(ターム)を表示する

function custom_post_edit_columns_custom_post($columns) {
    $columns['custom_post_category'] = 'カテゴリ';
    unset($columns['date']);
    $columns['date'] = '日時';
    return $columns;
}
add_filter( 'manage_edit-custom_post_columns', 'custom_post_edit_columns_custom_post' );

function custom_post_manage_column_custom_post($column_name, $post_id) {

    if( $column_name == 'custom_post_category' ) {
        $terms = get_the_terms( $post_id, 'custom_post_category' );
        $stitle = '';
        foreach($terms as $term){
            $stitle .= ' ' . $term->name;
        }

        $stitle = trim($stitle);
    }

    if ( isset($stitle) && $stitle ) {
        echo attribute_escape($stitle);
    }
}
add_action( 'manage_custom_post_posts_custom_column', 'custom_post_manage_column_custom_post', 10, 2 );