wp_insert_term

Sponsored Link

データベースに新しいタームを追加します。必要に応じて、既存のタームのエイリアスとしてそれをマークします。

Sponsored Link

エラー処理は、挿入する前に$taxonomyと$termパラメーターの非存在に割り当てられます。タームIDとタクソノミーの両方が既に存在している場合、配列はタームIDと返された内容を含めて返されます。配列のキーは数値を含んでいる’term_id’と’term_taxonomy_id’です。

このタームがまだ存在していない、または、上記が適用されることを前提としています。このタームは最初にタームテーブルに追加され、全てが順調であればタクソノミーに関連します。全てが正しい場合、いくつかのアクションはフィルターに先立って実行され、その後いくつかのアクションがフィルターが実行された後に実行されます。

引数はタームが$argsパラメーターに基づいてどのように処理されるかを決定します。以下は可能なオーバーライドとデフォルト値のリストです。

Adds a new term to the database. Optionally marks it as an alias of an existing term.

Error handling is assigned for the nonexistance of the $taxonomy and $term parameters before inserting. If both the term id and taxonomy exist previously, then an array will be returned that contains the term id and the contents of what is returned. The keys of the array are ‘term_id’ and ‘term_taxonomy_id’ containing numeric values.

It is assumed that the term does not yet exist or the above will apply. The term will be first added to the term table and then related to the taxonomy if everything is well. If everything is correct, then several actions will be run prior to a filter and then several actions will be run after the filter is run.

The arguments decide how the term is handled based on the $args parameter. The following is a list of the available overrides and the defaults.

‘alias_of’

デフォルトはありませんが、追加する場合、予想されるタームは別名であるスラッグです。文字列になると期待されます。

There is no default, but if added, expected is the slug that the term will be an alias of. Expected to be a string.

‘description’

デフォルト値はありません。存在する場合、タームと共にデータベースに追加されます。文字列になると期待されます。

There is no default. If exists, will be added to the database along with the term. Expected to be a string.

‘parent’

数値でデフォルトが0(ゼロ)であることが期待されます。タームの’parent’の値を代入します。

Expected to be numeric and default is 0 (zero). Will assign value of ‘parent’ to the term.

‘slug’

文字列になることを期待されます。デフォルトはsanitize_title($term)を使用することです。’slug’の引数が存在する場合、slugはそれが有効なタームでないかどうかを確認するためにチェックされます。そのチェックが成功した場合(それが有効なタームでない)、それが追加され、タームIDが付与されます。それが失敗した場合、チェックはタクソノミーが階層的であり親の引数がから出ないかどうかを行います。第二のチェックが成功した場合、タームが挿入されタームIDが与えられます。

Expected to be a string. The default is to use sanitize_title($term).
If ‘slug’ argument exists then the slug will be checked to see if it is not a valid term. If that check succeeds (it is not a valid term), then it is added and the term id is given. If it fails, then a check is made to whether the taxonomy is hierarchical and the parent argument is not empty. If the second check succeeds, the term will be inserted and the term id will be given.

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

<?php wp_insert_term( $term, $taxonomy, $args = array() ); ?>

wp_insert_termのパラメーター

$term

(int|string) (required)タームを追加または更新します。
デフォルト:なし

(int|string) (required) The term to add or update.
Default: None

$taxonomy

(string) (required)タームを追加するためのタクソノミーです。
デフォルト:なし

(string) (required) The taxonomy to which to add the term.
Default: None

$args

(array|string) (optional)タームを挿入された値を変更します。
デフォルト:なし

(array|string) (optional) Change the values of the inserted term
Default: None

戻り値

(array|WP_Error)

タームIDとタームタクソノミーID。(例:array(‘term_id’=>12,’term_taxonomy_id’=>34))

(array|WP_Error)
The Term ID and Term Taxonomy ID. (Example: array(‘term_id’=>12,’term_taxonomy_id’=>34))

$parent_term = term_exists( 'fruits', 'product' ); // array is returned if taxonomy is given
$parent_term_id = $parent_term['term_id']; // get numeric term id
wp_insert_term(
  'Apple', // the term 
  'product', // the taxonomy
  array(
    'description'=> 'A yummy apple.',
    'slug' => 'apple',
    'parent'=> $parent_term_id
  )
);

注意

フックが使用される

この関数は異なる5フックを呼び出します。

This function calls five different hooks:

  • create_term
  • create_taxonomy
  • term_id_filter
  • created_term
  • created_$taxonomy

全ての5つのフックはパラメーターとして、タームIDとタクソノミーIDを渡されます。

All five hooks are passed the term id and taxonomy id as parameters.

変更ログ

Since: 2.3.0

ソースファイル

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

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

関連ファンクションタグ

wp_update_term, wp_unique_term_slug

Sponsored Link