当サイト、Codex 日本語版は今後積極的な更新は行わない予定です。後継となる新ユーザーマニュアルは、https://ja.wordpress.org/support/ にあります。
万が一、当サイトで重大な問題を発見した際などは、フォーラムWordSlack #docs チャンネルでお知らせください。</p>

関数リファレンス/wp set post terms

提供: WordPress Codex 日本語版
< 関数リファレンス
2014年7月21日 (月) 23:35時点におけるGblsm (トーク | 投稿記録)による版 (en:Function Reference/wp_set_post_terms 13:18, 3 March 2014 Jdgrimes 版を流し込み。)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

このページ「関数リファレンス/wp set post terms」は未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

Description

Set terms for a post.

Uses wp_set_object_terms().

Usage

<?php wp_set_post_terms( $post_id, $terms, $taxonomy, $append ) ?>

Parameters

$post_id
integer) (required) Post ID.
初期値: 0
$terms
string,array) (optional) List of terms. Can be an array or a comma separated string. If you want to enter terms of a hierarchical taxonomy like categories, then use IDs. If you want to add non-hierarchical terms like tags, then use names.
初期値: array
$taxonomy
string) (optional) Possible values for example: 'category', 'post_tag', 'taxonomy slug'
初期値: post_tag
$append
boolean) (optional) If true, tags will be appended to the post. If false, tags will replace existing tags.
初期値: false

Return Values

(array|boolean|WP_Error|string) 
  • (array) An array of the terms affected if successful,
  • (boolean) false if integer value of $post_id evaluates as false (if ( ! (int) $post_id )),
  • (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).

Hooks

Examples

Notes

For hierarchical terms (such as categories), you must always pass the id rather than the term name to avoid confusion where there may be another child with the same name.

To get the term id you can use:

$term_id = term_exists( $term, $taxonomy, $parent );

You may also need to pass by reference:

wp_set_post_terms( $post_id, $term, &$taxonomy );

For non-hierarchical terms (such as tags), you can pass either the term name or id. If you pass the id there is only one caveat: You must pass it as an integer, and it must be in an array. This is necessary because any non-array value passed will be converted to a string, which will be interpreted as a term name.

$tag = '5'; // Wrong. This will add the tag with the *name* '5'.
$tag = 5; // Wrong. This will also add the tag with the name '5'.
$tag = array( '5' ); // Wrong. Again, this will be interpreted as a term name rather than an id.

$tag = array( 5 ); // Correct. This will add the tag with the id 5.
wp_set_post_terms( $post_id, $tag, $taxonomy );

This function will only work on the native post type. For a taxonomy on a custom post type use wp_set_object_terms()

Change Log

Since: 2.8.0

Source File

wp_set_post_terms() is located in wp-includes/post.php.

Related

タームタグ: 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(), the_terms(), get_object_taxonomies() is_object_in_taxonomy() the_taxonomies() wp_get_object_terms(), wp_set_object_terms(), wp_get_post_terms(), wp_set_post_terms(), wp_delete_object_term_relationships()


関数リファレンステンプレートタグ目次もご覧ください。


最新英語版: WordPress Codex » Function Reference/wp_set_post_terms最新版との差分