Help us understand the problem. What is going on with this article?

WordPress 管理画面でカスタム投稿タイプの投稿一覧にカスタムタクソノミーの絞込をつける

More than 1 year has passed since last update.

仕様

管理画面の投稿一覧のように、カスタム投稿タイプの投稿一覧でもカスタム分類による絞込のドロップダウンをつけたい
スクリーンショット 2017-02-03 18.21.54.png

余談だが、投稿一覧でカラムにカスタム分類を追加するのは register_taxonomy()show_admin_column 引数でできるのでよしなに(デフォルトではでないけど)
https://developer.wordpress.org/reference/functions/register_taxonomy/

正直 register_taxonomy()に引数あってもいいのになって思う。

コード

なお、WordPress 4.4移行。

フックする場所は restrict_manage_posts
https://developer.wordpress.org/reference/hooks/restrict_manage_posts/

ドロップダウンは wp_dropdown_categories() でオケ。
https://codex.wordpress.org/Function_Reference/wp_dropdown_categories

add_term_dropdown.php
<?php
add_action( 'restrict_manage_posts', 'add_term_dropdown', 10, 2 );
function add_term_dropdown( $post_type ) {
    if ( 'session' == $post_type ) { // カスタム投稿タイプ 'session' の場合
        $term_slug = get_query_var( 'session_track' );
        wp_dropdown_categories( array(
            'show_option_all'    => __( 'All Tracks', 'my_theme' ),
            'selected'           => $term_slug, // 絞り込んだあとそのタームが選択されている状態を維持
            'name'               => 'session_track', // select の name 属性
            'taxonomy'           => 'session_track', // カスタムタクソノミーのスラッグ
            'value_field'        => 'slug', // option の value属性の中身を何にするか
        ));
    }
}

wp_dropdown_categories() は優秀だよねってオチ。

現場からは以上です。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away