LoginSignup
5
4

More than 5 years have passed since last update.

Advanced Custom Fields の 'タクソノミー' を投稿画面でソートしたい

Posted at

目的

カスタムタクソノミーを ACFの タクソノミー フィールドで表示したいが、表示順をIDの昇順に変更したい

解決策

下記の通りフィルターを用意する。テーマの functions.php でも、独自にプラグインを用意しても良い。

function chatii_taxonomy_sort( $args, $field ) {
    $args['orderby'] = 'id';
    $args['order'] = 'ASC';

    return $args;
}

add_filter('acf/fields/taxonomy/wp_list_categories/name={{custom_field_name}}', 'chatii_taxonomy_sort', 10, 3);

({{custom_field_name}} は 用意したカスタムフィールドの フィールド名)

$args は、wp_list_categories() の取る引数と同じなので、上書きしてやればソート順を変えられる

記事ではないので ID ではなく、idになる。下記ドキュメント参照。

つまづきポイント

タクソノミー フィールドが チェックボックス または ラジオボタン の場合は、acf/fields/taxonomy/wp_list_categories を使うが、セレクトボックス または 複数選択 の場合は acf/fields/taxonomy/query を使う

ドキュメント

5
4
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
5
4