
カスタム投稿の記事一覧にカスタムタクソノミーで絞り込みするためのフィルターを追加する覚書。
通常は日付でしか絞り込めないが下記のフックでフィルターを追加できる。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// 例としてカスタム投稿「comic」にタクソノミー「series」での絞り込みを追加 function add_filter_post_type_by_taxonomy() { global $post_type; if ( !in_array($post_type, ['comic']) ) return; $taxonomy = 'series'; $terms = get_terms($taxonomy); if ( empty($terms) ) return; $selected = get_query_var($taxonomy); $options = ''; foreach ($terms as $term) { $options .= sprintf('<option value="%s" %s>%s</option>' ,$term->slug ,($selected==$term->slug) ? 'selected="selected"' : '' ,$term->name ); } $select = '<select name="%s"><option value="">すべてのシリーズ</option>%s</select>'; printf($select, $taxonomy, $options); } add_action('restrict_manage_posts','add_filter_post_type_by_taxonomy'); |