0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WordPressの投稿ページ(管理画面)でタクソノミー選択チェックボックスの並び順を変更する方法をご説明します。

方法1: 管理画面専用のフィルター

function custom_admin_taxonomy_order($args, $taxonomies) {
    // 管理画面でのみ適用
    if (!is_admin()) {
        return $args;
    }
    
    // 投稿編集画面でのみ適用
    global $pagenow;
    if (!in_array($pagenow, array('post.php', 'post-new.php'))) {
        return $args;
    }
    
    $target_taxonomies = array('your_taxonomy_name'); // タクソノミー名を指定
    
    if (array_intersect($target_taxonomies, $taxonomies)) {
        $args['orderby'] = 'description'; // description順
        $args['order'] = 'ASC';
    }
    
    return $args;
}
add_filter('get_terms_args', 'custom_admin_taxonomy_order', 10, 2);

方法2: 特定のタクソノミーメタボックスのみ対象

function custom_taxonomy_metabox_order() {
    add_filter('get_terms_args', function($args, $taxonomies) {
        // 現在の画面がpost編集画面かチェック
        $screen = get_current_screen();
        if (!$screen || !in_array($screen->base, array('post'))) {
            return $args;
        }
        
        // 特定のタクソノミーのみ対象
        $target_taxonomies = array('your_taxonomy_name');
        
        if (array_intersect($target_taxonomies, $taxonomies)) {
            $args['orderby'] = 'description';
            $args['order'] = 'ASC';
        }
        
        return $args;
    }, 10, 2);
}
add_action('admin_init', 'custom_taxonomy_metabox_order');

方法3: 複数のタクソノミーで異なる並び順

function admin_taxonomy_orders($args, $taxonomies) {
    if (!is_admin()) {
        return $args;
    }
    
    global $pagenow;
    if (!in_array($pagenow, array('post.php', 'post-new.php'))) {
        return $args;
    }
    
    // タクソノミーごとに並び順を指定
    $taxonomy_orders = array(
        'category' => array('orderby' => 'name', 'order' => 'ASC'),
        'post_tag' => array('orderby' => 'count', 'order' => 'DESC'),
        'your_taxonomy_name' => array('orderby' => 'description', 'order' => 'ASC'),
    );
    
    foreach ($taxonomies as $taxonomy) {
        if (isset($taxonomy_orders[$taxonomy])) {
            $args = array_merge($args, $taxonomy_orders[$taxonomy]);
            break;
        }
    }
    
    return $args;
}
add_filter('get_terms_args', 'admin_taxonomy_orders', 10, 2);

方法4: JavaScriptで並び替え(より確実)

function custom_taxonomy_sort_script() {
    global $pagenow;
    if (in_array($pagenow, array('post.php', 'post-new.php'))) {
        ?>
        <script type="text/javascript">
        jQuery(document).ready(function($) {
            // 特定のタクソノミーのチェックボックスを並び替え
            var taxonomyDiv = $('#your_taxonomy_namediv'); // タクソノミー名に変更
            
            if (taxonomyDiv.length) {
                var checkboxes = taxonomyDiv.find('.selectit');
                
                // ラベルのテキスト(description)で並び替え
                checkboxes.sort(function(a, b) {
                    var aText = $(a).find('label').text().trim();
                    var bText = $(b).find('label').text().trim();
                    return aText.localeCompare(bText);
                });
                
                // 並び替え後のリストを再構築
                var checklist = taxonomyDiv.find('.categorychecklist');
                checklist.empty().append(checkboxes);
            }
        });
        </script>
        <?php
    }
}
add_action('admin_footer', 'custom_taxonomy_sort_script');

方法5: カスタムフィールドの値で並び替え

function custom_taxonomy_order_by_custom_field($args, $taxonomies) {
    if (!is_admin()) {
        return $args;
    }
    
    global $pagenow;
    if (!in_array($pagenow, array('post.php', 'post-new.php'))) {
        return $args;
    }
    
    $target_taxonomies = array('your_taxonomy_name');
    
    if (array_intersect($target_taxonomies, $taxonomies)) {
        // メタクエリでカスタムフィールド順に並び替え
        $args['meta_key'] = 'custom_order_field';
        $args['orderby'] = 'meta_value_num';
        $args['order'] = 'ASC';
    }
    
    return $args;
}
add_filter('get_terms_args', 'custom_taxonomy_order_by_custom_field', 10, 2);

使用方法

  1. your_taxonomy_name を実際のタクソノミー名に変更
  2. orderby の値を希望の並び順に変更:
  • name: 名前順
  • description: 説明順
  • count: 投稿数順
  • term_id: ID順
  • slug: スラッグ順

最も確実なのは方法4のJavaScriptを使った方法です。WordPressのキャッシュやフィルターの競合を避けられます。​​​​​​​​​​​​​​​​

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?