追加したカスタム投稿の一覧に、タクソノミーターム名(カテゴリー名)と絞り込み検索を追加するコードです。
/**
* カスタム投稿一覧に列追加
*
*/
function add_custom_column( $defaults ) {
$defaults['タクソノミー名'] = 'カテゴリー';
return $defaults;
}
add_filter('manage_posts_columns', 'add_custom_column');
/**
* カスタム投稿一覧にタクソノミーのタームを表示
*
*/
function add_custom_column_id($column_name, $id) {
if( $column_name == 'タクソノミー名' ) {
echo get_the_term_list($id, 'タクソノミー名', '', ', ');
}
}
add_action('manage_posts_custom_column', 'add_custom_column_id', 10, 2);
/**
* カスタム投稿一覧にタームで絞り込むプルダウンリストを表示(3階層まで)
*
*/
function add_post_taxonomy_restrict_filter() {
global $post_type;
if ( 'experience' == $post_type ) {
echo '<select name="experience-cat">';
echo '<option value="">カテゴリー指定なし</option>';
$terms = get_terms('タクソノミー名', 'hide_empty=0');
foreach ($terms as $term) :
if ($term->parent == 0):
echo '<option value="' . $term->slug . '">' . $term->name . '</option>';
$parentID = $term->term_id;
$children = get_terms( 'タクソノミー名','hierarchical=1&parent='.$parentID );
if(!empty($children)):
foreach($children as $child):
echo '<option value="' . $child->slug . '"> ' . $child->name . '</option>';
$grandchildren = get_terms('experience-cat','hierarchical=0&parent='.$child->term_id);
if(!empty($grandchildren)):
foreach ( $grandchildren as $grandson ):
echo '<option value="' . $grandson->slug . '"> ' . $grandson->name . '</option>';
endforeach;
endif;
endforeach;
endif;
endif;
endforeach;
echo '</select>';
}
}
add_action( 'restrict_manage_posts', 'add_post_taxonomy_restrict_filter' );



