WP: 投稿一覧にカスタム分類の絞り込み検索を表示
WordPress、投稿一覧をタクソノミーで絞り込む。管理画面の投稿一覧で検索オプションリストのカスタマイズ。
管理画面のカスタマイズ
管理画面のカスタマイズ
投稿一覧にカスタム分類の絞り込み検索を表示:
Keyword:カスタム分類
管理画面のカスタマイズ
投稿一覧にカスタム分類の絞り込み検索を表示
管理画面のカスタマイズ
投稿一覧にカスタム分類の絞り込み検索を表示:
function.php
<?php
// カスタム投稿画面にフィルター用のドロップダウンリストを追加
add_action('restrict_manage_posts', function() {
global $post_type;
if ( !in_array($post_type, ['post']) ) return;
$taxonomy = 'area';
// タームの親子構造の配列を取得
$tree_terms = wp_dropdown_categories('taxonomy=region&hierarchical=1&echo=0&value_field=slug');
//print_r($tree_terms);
// slugとnameを抜き出し配列に入れる
preg_match_all('/value="(.*?)">(.*?)</', $tree_terms, $matches);
//print_r($matches);
//print_r($matches[2]);
$terms = get_terms($taxonomy);
//print_r($terms[0]->name);
// 親子構造にタームを並べ替え
$order = $matches[1];
$array1 = $terms;
$sorted_array = [];
foreach ($order as $i => $search_id) {
foreach ($array1 as $idx => $record) {
if ($search_id == $record->slug) {
$record->name = $matches[2][$i];
$sorted_array[] = $record;
unset($array1[$idx]);
break;
}
}
}
$array1 = $sorted_array;
//print_r($array1);
$terms = $sorted_array;
// ドロップダウン出力
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);
});
?>Keyword:カスタム分類
