register_taxonomy

Sponsored Link

この関数は、タクソノミーを追加または上書きします。名前、オブジェクト名、およびパラメーターの配列を受け取ります。それは何も返しません。

Sponsored Link

This function adds or overwrites a taxonomy. It takes in a name, an object name that it affects, and an array of parameters. It does not return anything.

他のタクソノミー、投稿タイプ、予約済みのWordPressの公開、プライベートのクエリ変数と競合しないようにタクソノミー名を選択する際に使用して注意しなければなりません。それらの完全なリストは、予約済のタームセクションで説明されています。特に、大文字は避けるべきです(これは3.0で許可されていますが、”Cheatin”エラーで、3.1まで適用されませんでした)。

Care should be used in selecting a taxonomy name so that it does not conflict with other taxonomies, post types, and reserved WordPress public and private query variables. A complete list of those is described in the Reserved Terms section. In particular, capital letters should be avoided (This was allowed in 3.0, but not enforced until 3.1 with the “Cheatin'” error).

register_taxonomyのファンクションタグ使用方法

<?php register_taxonomy( $taxonomy, $object_type, $args ); ?>

この関数を呼び出すにはinitアクションを使用します。アクションの外側からそれを呼び出すと、トラブルに繋がることになります。詳細については#15568を参照してください。

Use the init action to call this function. Calling it outside of an action can lead to troubles. See #15568 for details.

register_taxonomyのパラメーター

$taxonomy

(string) (required)タクソノミーの名前。名前はスラッグのフォーマットであり(大文字やスペースを含めることはできません)、32文字長さ以上ではない(データベース構造の制限)
デフォルト:なし

(string) (required) The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces) and not more than 32 characters long (database structure restriction).
Default: None

$object_type

(array/string) (required)タクソノミーオブジェクトのオブジェクトタイプの名前。オブジェクトタイプは投稿タイプまたは登録されているかもしれない任意のカスタム投稿タイプにビルトインすることができます。
デフォルト:なし

(array/string) (required) Name of the object type for the taxonomy object. Object-types can be built-in Post Type or any Custom Post Type that may be registered.
Default: None

ビルトイン投稿タイプ:

Builtin Post Types:

  • post
  • page
  • attachment
  • revision
  • nav_menu_item

カスタム投稿タイプ:

Custom Post Types:

  • {custom_post_type} – カスタム投稿タイプ名は全て小文字でスペースなしにしなければなりません。
  • null – 登録されたタクソノミーを明示的にnullに設定しますが、任意のオブジェクトに関連付けることはありませんので、管理UI内で直接使用することはできません。あなたが手動でカスタム投稿タイプ(register_post_type()を参照)またはregister_taxonomy_for_object_type()を使用して登録する時に($argsを挿入した)’taxonomy’パラメーターを使用して登録する必要があります。
  • {custom_post_type} – Custom Post Type names must be all in lower-case and without any spaces.
  • null – Setting explicitly to null registers the taxonomy but doesn’t associate it with any objects, so it won’t be directly available within the Admin UI. You will need to manually register it using the ‘taxonomy’ parameter (passed through $args) when registering a custom post_type (see register_post_type()), or using register_taxonomy_for_object_type().

$args

(array/string) (optional)引数の配列
デフォルト:なし

(array/string) (optional) An array of Arguments.
Default: None

引数

label

(string) (optional)翻訳の為にマークされたタクソノミーの複数の記述名。
デフォルト:$labels->nameで上書き

(string) (optional) A plural descriptive name for the taxonomy marked for translation.
Default: overridden by $labels->name

labels

(array) (optional) labels – このタクソノミーのラベルの配列。デフォルトでは、タグのラベルは非階層的なタイプと1つの階層にカテゴリーラベルを使用します。
デフォルト:空の場合、名前はラベル値に設定されており、singular_nameは名前の値に設定されています。

(array) (optional) labels – An array of labels for this taxonomy. By default tag labels are used for non-hierarchical types and category labels for hierarchical ones.
Default: if empty, name is set to label value, and singular_name is set to name value

  • ‘name’ – タクソノミーの一般的な名前は、通常複数形です。$tax->labelで上書きするのと同じです。デフォルトは_x(’Post Tags’、’タクソノミーの一般名’)または_x(’Categories’, ‘タクソノミーの一般名’)。この文字列を国際化する時、あなたの投稿タイプに一致しているgettextのcontextを使用してください。例:_x(‘Writers’, ‘タクソノミーの一般名’);
  • ‘singular_name’ – このタクソノミーの1つのオブジェクトのための名前。デフォルトは_x( ‘Post Tag’, ‘タクソノミーの単数形の名前’ )または_x(’Category’, ‘タクソノミーの単数形の名前’)。この文字列を国際化する時、あなたの投稿タイプに一致するgettextのcontextを使用してください。例:_x(‘Writer’, ‘タクソノミーの単数形の名前’);
  • ‘menu_name’ – メニュー名のテキスト。この文字列はメニューアイテムを取得するための名前です。デフォルトは名前の値です。
  • ‘all_items’ – 全ての項目のテキスト。デフォルトは __( ‘All Tags’ ) または __( ‘All Categories’ )
  • ‘edit_item’ – 項目のテキストを編集する。 デフォルトは __( ‘Edit Tag’ ) または __( ‘Edit Category’ )
  • ‘update_item’ – 項目のテキストをアップデートする。デフォルトは __( ‘Update Tag’ ) または __( ‘Update Category’ )
  • ‘add_new_item’ – 新しい項目のテキストを追加します。デフォルトは __( ‘Add New Tag’ ) または __( ‘Add New Category’ )
  • ‘new_item_name’ – 新しい項目の名前のテキスト。デフォルトは __( ‘New Tag Name’ ) または __( ‘New Category Name’ )
  • ‘parent_item’ – 親の項目のテキスト。 この文字列は投稿タグと同じように非階層型のタクソノミーを使用しません。デフォルトは、null または __( ‘Parent Category’ )
  • ‘parent_item_colon’ – parent_itemと同じですが、コロンを使用します:最後のnullで、 __( ‘Parent Category:’ )
  • ‘search_items’ – 検索の項目テキスト。デフォルトは __( ‘Search Tags’ ) または __( ‘Search Categories’ )
  • ‘popular_items’ – 人気の項目テキスト。 デフォルトは __( ‘Popular Tags’ ) または null
  • ‘separate_items_with_commas’ – タクソノミーのメタボックスで使用しているカンマテキストを持つ独立した項目。この文字列は階層的なタクソノミーで使用しません。デフォルトは __( ‘Separate tags with commas’ ), または null
  • ‘add_or_remove_items’ – 項目テキストを追加したり削除し、Javascriptが無効になっている時、メタボックスで使用します。この文字列は非階層的なタクソノミーで使用しません。デフォルトは __( ‘Add or remove tags’ ) または null
  • ‘choose_from_most_used’ – タクソノミーのメタボックスで使用される最も使用されるテキストから選択肢ます。この文字列は階層的なタクソノミーでは使用しません。デフォルトは __( ‘Choose from the most used tags’ ) または null
  • ‘name’ – general name for the taxonomy, usually plural. The same as and overridden by $tax->label. Default is _x( ‘Post Tags’, ‘taxonomy general name’ ) or _x(‘Categories’, ‘taxonomy general name’ ). When internationalizing this string, please use a gettext context matching your post type. Example: _x(‘Writers’, ‘taxonomy general name’);
  • ‘singular_name’ – name for one object of this taxonomy. Default is _x( ‘Post Tag’, ‘taxonomy singular name’ ) or _x( ‘Category’, ‘taxonomy singular name’ ). When internationalizing this string, please use a gettext context matching your post type. Example: _x(‘Writer’, ‘taxonomy singular name’);
  • ‘menu_name’ – the menu name text. This string is the name to give menu items. Defaults to value of name
  • ‘all_items’ – the all items text. Default is __( ‘All Tags’ ) or __( ‘All Categories’ )
  • ‘edit_item’ – the edit item text. Default is __( ‘Edit Tag’ ) or __( ‘Edit Category’ )
  • ‘update_item’ – the update item text. Default is __( ‘Update Tag’ ) or __( ‘Update Category’ )
  • ‘add_new_item’ – the add new item text. Default is __( ‘Add New Tag’ ) or __( ‘Add New Category’ )
  • ‘new_item_name’ – the new item name text. Default is __( ‘New Tag Name’ ) or __( ‘New Category Name’ )
  • ‘parent_item’ – the parent item text. This string is not used on non-hierarchical taxonomies such as post tags. Default is null or __( ‘Parent Category’ )
  • ‘parent_item_colon’ – The same as parent_item, but with colon : in the end null, __( ‘Parent Category:’ )
  • ‘search_items’ – the search items text. Default is __( ‘Search Tags’ ) or __( ‘Search Categories’ )
  • ‘popular_items’ – the popular items text. Default is __( ‘Popular Tags’ ) or null
  • ‘separate_items_with_commas’ – the separate item with commas text used in the taxonomy meta box. This string isn’t used on hierarchical taxonomies. Default is __( ‘Separate tags with commas’ ), or null
  • ‘add_or_remove_items’ – the add or remove items text and used in the meta box when JavaScript is disabled. This string isn’t used on hierarchical taxonomies. Default is __( ‘Add or remove tags’ ) or null
  • ‘choose_from_most_used’ – the choose from most used text used in the taxonomy meta box. This string isn’t used on hierarchical taxonomies. Default is __( ‘Choose from the most used tags’ ) or null

public

(boolean) (optional)このタクソノミーは、管理UIで公開される必要があります。
デフォルト:true

(boolean) (optional) Should this taxonomy be exposed in the admin UI.
Default: true

show_ui

(boolean) (optional) このタクソノミーを管理するためのデフォルトのUIを生成するかどうか。
デフォルト:設定されていない場合は、公開引数の値がデフォルトになります。 3.5の時点では、添付ファイルのタクソノミーがUIを非表示にするためにfalseに設定します。

(boolean) (optional) Whether to generate a default UI for managing this taxonomy.
Default: if not set, defaults to value of public argument. As of 3.5, setting this to false for attachment taxonomies will hide the UI.

show_in_nav_menus

(boolean) (optional) trueはこのタクソノミーがナビゲーション・メニュー内で選択するために利用できるようになります。
デフォルト:設定されていない場合、公開引数の値がデフォルトになります。

(boolean) (optional) true makes this taxonomy available for selection in navigation menus.
Default: if not set, defaults to value of public argument

show_tagcloud

(boolean) (optional)タグクラウドウィジェットはこのタクソノミーを使用する事を許可するかどうか。
デフォルト:設定されていない場合、show_ui引数の値がデフォルトになります。

(boolean) (optional) Whether to allow the Tag Cloud widget to use this taxonomy.
Default: if not set, defaults to value of show_ui argument

show_admin_column

(boolean) (optional)投稿タイプに関連したタクソノミーカラムの自動生成を許可するかどうか。(3.5以降で利用可能)
デフォルト:false

(boolean) (optional) Whether to allow automatic creation of taxonomy columns on associated post-types. (Available since 3.5)
Default: false

hierarchical

(boolean) (optional)このタクソノミーの階層(子孫を持つ)は、カテゴリーのような、あるいは、タグのような階層構造ではありません。
デフォルト:false

(boolean) (optional) Is this taxonomy hierarchical (have descendants) like categories or not hierarchical like tags.
Default: false

update_count_callback

(string) (optional) 投稿など、更新される、$object_typeに関連するカウントと呼ばれる関数名です。フックと同じように動作します。
デフォルト:なし

(string) (optional) A function name that will be called when the count of an associated $object_type, such as post, is updated. Works much like a hook.
Default: None

query_var

(boolean or string) (optional)タクソノミーの”name”である$taxonomyがデフォルトの代わりにカスタムquery_var を使用するための文字列として設定する、query_varを無効にする場合はFalse。
デフォルト:$taxonomy

(boolean or string) (optional) False to disable the query_var, set as string to use custom query_var instead of default which is $taxonomy, the taxonomy’s “name”.
Default: $taxonomy

注意:query_varは、新しいWP_Query(array(‘people’=>$person_name)) とURLクエリーの/?people=$person_nameのようにWP_Queryを通して直接クエリーに使用されています。falseにquery_varを設定すると、これらのメソッドは無効になりますが、あなたがWP_Query(array(‘taxonomy’=>’people’, ‘term’=>$person_name))のようにWP_Queryのタクソノミークエリーを明示的に記事を取得することができます。

Note: The query_var is used for direct queries through WP_Query like new WP_Query(array(‘people’=>$person_name)) and URL queries like /?people=$person_name. Setting query_var to false will disable these methods, but you can still fetch posts with an explicit WP_Query taxonomy query like WP_Query(array(‘taxonomy’=>’people’, ‘term’=>$person_name)).

rewrite

(boolean/array) (optional)別名”きれいなパーマリンク”を自動URL書き換えを防止するためにfalseに設定します。以下に概説するように、パーマリンクのデフォルトのURL設定を上書きするには$args配列を渡します。
デフォルト:true

(boolean/array) (optional) Set to false to prevent automatic URL rewriting a.k.a. “pretty permalinks”. Pass an $args array to override default URL settings for permalinks as outlined below:
Default: true

  • ‘slug’ – キレイなパーマリンクテキスト(例 /tag/)として使用します。 – デフォルトに$taxonomy(タクソノミー名のスラッグ)
  • ‘with_front’ – フロントベースを付加することができるパーマリンクを許可します。 – デフォルトじゃtrue
  • ‘hierarchical’ – 階層URLを許可するtrueあるいはfalse(バージョン3.1に実装されている) – デフォルトはfalse
  • ‘ep_mask’ – このタクソノミーにエンドポイント·マスクを割り当てます。 – EP_NONEがデフォルトになります。詳細情報については、このエンドポイントのWordPressのプラグインの概要を確認してください。
  • ‘slug’ – Used as pretty permalink text (i.e. /tag/) – defaults to $taxonomy (taxonomy’s name slug)
  • ‘with_front’ – allowing permalinks to be prepended with front base – defaults to true
  • ‘hierarchical’ – true or false allow hierarchical urls (implemented in Version 3.1) – defaults to false
  • ‘ep_mask’ – Assign an endpoint mask for this taxonomy – defaults to EP_NONE. For more info see this Make WordPress Plugins summary of endpoints.

注:これを変更した後で書き換えルールをフラッシュする必要があるかもしれません。あなたはパーマリンク設定ページに移動してルールを再保存することより手動でそれを行うことができます。ーーあなたはタームを変更する必要はありません。ーーまたは$wp_rewrite->flush_rules()を呼び出すことによって。あなたは一度タクソノミーを作成された後にルールをフラッシュすべきであり、毎回プラグイン/テーマの負荷ではありません。

Note: You may need to flush the rewrite rules after changing this. You can do it manually by going to the Permalink Settings page and re-saving the rules — you don’t need to change them — or by calling $wp_rewrite->flush_rules(). You should only flush the rules once after the taxonomy has been created, not every time the plugin/theme loads.

capabilities

(array) (optional)このタクソノミーのための機能の配列です。
デフォルト:なし

(array) (optional) An array of the capabilities for this taxonomy.
Default: None

  • ‘manage_terms’ – ‘manage_categories’
  • ‘edit_terms’ – ‘manage_categories’
  • ‘delete_terms’ – ‘manage_categories’
  • ‘assign_terms’ – ‘edit_posts’

sort

(boolean) (optional)このタクソノミーは、タームがオブジェクトに追加される順番を覚えるべきかどうかを指定します。
デフォルト:なし

(boolean) (optional) Whether this taxonomy should remember the order in which terms are added to objects.
Default: None

_builtin

(boolean) (not for general use)このタクソノミーがネイティブまたは”ビルトイン”タクソノミーであるかどうか。注:このCODEXエントリーはドキュメンテーションのためです。ーコア開発者が独自のタクソノミーを登録する時に、これを使用しないことをオススメします。
デフォルト:false

(boolean) (not for general use) Whether this taxonomy is a native or “built-in” taxonomy. Note: this Codex entry is for documentation – core developers recommend you don’t use this when registering your own taxonomy
Default: false

“book”という投稿タイプのために、ジャンルと作家の2つのタクソノミーを登録する例です(バージョン3.1の引数を使用しています):

注:あなたがテーマのfunctions.phpのテンプレートファイルにカスタムタクソノミーを定義することができます。

An example of registering a two taxonomies, genres and writers, for the post type called “book” (uses Version 3.1 arguments):

Note: You can define custom taxonomies in a themes’s functions.php template file:

<?php
//hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_book_taxonomies', 0 );

//create two taxonomies, genres and writers for the post type "book"
function create_book_taxonomies() 
{
  // Add new taxonomy, make it hierarchical (like categories)
  $labels = array(
    'name'                => _x( 'Genres', 'taxonomy general name' ),
    'singular_name'       => _x( 'Genre', 'taxonomy singular name' ),
    'search_items'        => __( 'Search Genres' ),
    'all_items'           => __( 'All Genres' ),
    'parent_item'         => __( 'Parent Genre' ),
    'parent_item_colon'   => __( 'Parent Genre:' ),
    'edit_item'           => __( 'Edit Genre' ), 
    'update_item'         => __( 'Update Genre' ),
    'add_new_item'        => __( 'Add New Genre' ),
    'new_item_name'       => __( 'New Genre Name' ),
    'menu_name'           => __( 'Genre' )
  ); 	

  $args = array(
    'hierarchical'        => true,
    'labels'              => $labels,
    'show_ui'             => true,
    'show_admin_column'   => true,
    'query_var'           => true,
    'rewrite'             => array( 'slug' => 'genre' )
  );

  register_taxonomy( 'genre', array( 'book' ), $args );

  // Add new taxonomy, NOT hierarchical (like tags)
  $labels = array(
    'name'                         => _x( 'Writers', 'taxonomy general name' ),
    'singular_name'                => _x( 'Writer', 'taxonomy singular name' ),
    'search_items'                 => __( 'Search Writers' ),
    'popular_items'                => __( 'Popular Writers' ),
    'all_items'                    => __( 'All Writers' ),
    'parent_item'                  => null,
    'parent_item_colon'            => null,
    'edit_item'                    => __( 'Edit Writer' ), 
    'update_item'                  => __( 'Update Writer' ),
    'add_new_item'                 => __( 'Add New Writer' ),
    'new_item_name'                => __( 'New Writer Name' ),
    'separate_items_with_commas'   => __( 'Separate writers with commas' ),
    'add_or_remove_items'          => __( 'Add or remove writers' ),
    'choose_from_most_used'        => __( 'Choose from the most used writers' ),
    'menu_name'                    => __( 'Writers' )
  ); 

  $args = array(
    'hierarchical'            => false,
    'labels'                  => $labels,
    'show_ui'                 => true,
    'show_admin_column'       => true,
    'update_count_callback'   => '_update_post_term_count',
    'query_var'               => true,
    'rewrite'                 => array( 'slug' => 'writer' )
  );

  register_taxonomy( 'writer', 'book', $args );
}
?>

基本的な例

add_action( 'init', 'create_book_tax' );

function create_book_tax() 
{
   register_taxonomy(
      'genre',
      'book',
      array(
         'label' => __( 'Genre' ),
         'rewrite' => array( 'slug' => 'genre' ),
         'hierarchical' => true
      )
   );
}

注:カスタムタクソノミーがタグのように行動することを確認したい場合、あなたがオプション’update_count_callback’ => ‘_update_post_term_count’を追加しなければなりません。そうしないと、複数のカンマで区切られたアイテムは直ぐに追加される結果は、別々の値としてではなく、単体の値として保存されます。

Note: If you want to ensure that your custom taxonomy behaves like a tag, you must add the option ‘update_count_callback’ => ‘_update_post_term_count’. Not doing so will result in multiple comma-separated items added at once being saved as a single value, not as separate values. This can cause undue stress when using get_the_term_list and other term display functions.

予約済みのターム

  • attachment
  • attachment_id
  • author
  • author_name
  • calendar
  • cat
  • category
  • category__and
  • category__in
  • category__not_in
  • category_name
  • comments_per_page
  • comments_popup
  • cpage
  • day
  • debug
  • error
  • exact
  • feed
  • hour
  • link_category
  • m
  • minute
  • monthnum
  • more
  • name
  • nav_menu
  • nopaging
  • offset
  • order
  • orderby
  • p
  • page
  • page_id
  • paged
  • pagename
  • pb
  • perm
  • post
  • post__in
  • post__not_in
  • post_format
  • post_mime_type
  • post_status
  • post_tag
  • post_type
  • posts
  • posts_per_archive_page
  • posts_per_page
  • preview
  • robots
  • s
  • search
  • second
  • sentence
  • showposts
  • static
  • subpost
  • subpost_id
  • tag
  • tag__and
  • tag__in
  • tag__not_in
  • tag_id
  • tag_slug__and
  • tag_slug__in
  • taxonomy
  • tb
  • term
  • type
  • w
  • withcomments
  • withoutcomments
  • year

変更ログ

  • 3.5.0:
    • Setting ‘show_ui’ to false hides UI for attachment taxonomies.
    • Add ‘show_admin_column’ to allow automatic creation of taxonomy columns on associated post types.
  • Since: 2.3.0

ソースファイル

register_taxonomy()は、wp-includes/taxonomy.php内に位置しています。

register_taxonomy() is located in wp-includes/taxonomy.php.

リソース

関連ファンクションタグ

register_post_type()

Sponsored Link