wp_set_object_terms

Sponsored Link

タームとタクソノミータイプ(タグ、カテゴリー、など)にオブジェクト(投稿、リンクなど)関するものです。それが既に存在しない場合、タームとタクソノミーのリレーションシップを作成します。

Sponsored Link

タームはタクソノミーでグループ化または分類に属していることを意味しています。タームは下に存在しているタクソノミーを定義することによって、文脈を取得するするまで意味を持ちません。

Relates an object (post, link etc) to a term and taxonomy type (tag, category, etc). Creates the term and taxonomy relationship if it doesn’t already exist.

A relationship means that the term is grouped in or belongs to the taxonomy. A term has no meaning until it is given context by defining which taxonomy it exists under.

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

<?php wp_set_object_terms( $object_id, $terms, $taxonomy, $append ) ?>

wp_set_object_termsのパラメーター

$object_id

(int) (required) 投稿記事のような関連するためのオブジェクトです。
デフォルト:なし

(int) (required) The object to relate to, such as post ID.
Default: None

$terms

(array/int/string) (required)ターム(カテゴリーあるいはタグIDなど)のスラッグまたはIDが、このタクソノミー内に関連するタームに全て置き換えられます。オブジェクトから全てのタームをクリアまたは削除するには、空の文字列またはNULLを渡します。整数はタグIDとして解釈されます。
デフォルト:なし

(array/int/string) (required) The slug or id of the term (such as category or tag IDs), will replace all existing related terms in this taxonomy. To clear or remove all terms from an object, pass an empty string or NULL. Integers are interpreted as tag IDs.
Default: None

$taxonomy

(array/string) (required)オブジェクトにタームを関連するためのコンテキスト。これはカテゴリー、post_tag、または別のタクソノミーの名前を指定できます。
デフォルト:なし

(array/string) (required) The context in which to relate the term to the object. This can be category, post_tag, or the name of another taxonomy.
Default: None

$append

(bool) (required)trueの場合、タグがオブジェクトに追加されます。falseの場合、タグは既存のタグに置き換えられます。
デフォルト:False

(bool) (required) If true, tags will be appended to the object. If false, tags will replace existing tags
Default: False

Returns

(mixed)

  • (array) 成功した場合に影響を受ける条件の配列
  • (WP_Error) 無効なタクソノミーのWordPress Errorオブジェクト(‘invalid_taxonomy’)。
  • (string) 最初に問題が起こっているタームが$termsパラメーターに誤って名付けられているタームを取得した場合(無効なタームIDが認められ挿入されている)。
  • (array) An array of the terms affected if successful,
  • (WP_Error) The WordPress Error object on invalid taxonomy (‘invalid_taxonomy’).
  • (string) The first offending term if a term given in the $terms parameter is named incorrectly. (Invalid term ids are accepted and inserted).

42のIDを持つ投稿にカテゴリーを追加したい場合:

If you wanted to add a categories to a post with the ID of 42:

<?php
$cat_ids = array( 6,8 );
    //to make sure the terms IDs is integers:
    //$cat_ids = array_map('intval', $cat_ids);
    //$cat_ids = array_unique( $cat_ids );
wp_set_object_terms( '42', $cat_ids, 'category' );
?>

42のIDを持つ投稿から全てのカテゴリーをクリア/削除したい場合:

If you wanted to clear/remove all categories from a post with the ID of 42:

<?php wp_set_object_terms( '42', NULL, 'category' ); ?>

変更ログ

  • 3.5.0: Return WP_Error object upon database insertion failure.
  • Since: 2.3.0

注意

恐らく、wp_set_post_terms()は、コンマで区切られたタクソノミーを変換して、整数における階層的なタームを検証している値をチェックするため、より多くの有能な関数です。

Perhaps the wp_set_post_terms() is a more useful function, since it checks the values​​, converting taxonomies separated by commas and validating hierarchical terms in integers.

ソースファイル

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

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

関連ファンクションタグ

Terms: is_term(), term_exists(), get_objects_in_term(), get_term(), get_term_by(), get_term_children(), get_term_link(), get_terms(), get_the_terms(), get_the_term_list(), has_term(), sanitize term(), wp_get_object_terms(), wp_set_object_terms(), wp_get_post_terms(), wp_set_post_terms()

Sponsored Link