• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

関数リファレンス/add post meta

提供: WordPress Codex 日本語版
< 関数リファレンス
2010年4月21日 (水) 23:51時点におけるKz (トーク | 投稿記録)による版 (en:Function Reference/add post meta 13:18, 9 November 2009 GeertDD)

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

Description

add_post_meta adds a custom (meta) field to the specified post (or page).

If the $unique parameter is set to true and the specified meta key already exists, the function returns false and makes no changes; otherwise, it returns true.

Usage

%%% <?php add_post_meta($post_id, $meta_key, $meta_value, $unique); ?> %%%

Parameters

$post_id
integer) (必須) The ID of the post to which you will add a custom field.
初期値: なし
$meta_key
string) (必須) The key of the custom field you will add.
初期値: なし
$meta_value
string) (必須) The value of the custom field you will add.
初期値: なし
$unique
boolean) (optional) Whether or not you want the key to be unique. When set to true, this will ensure that there is not already a custom field attached to the post with $meta_key as it's key, and, if such a field already exists, the key will not be added.
初期値: false

Examples

Default Usage

<?php add_post_meta(68, 'my_key', 47); ?>

Adding or updating a unique field

Adds a new field if the key doesn't exist, or updates the existing field. (UPDATE: If the fourth parameter of add_post_meta is set to true, the field will not be updated if it already exists (tested on WP 2.6.2). Use if (!update_post_meta(...)) add_post_meta(...)).

 <?php add_post_meta(7, 'fruit', 'banana', true) or update_post_meta(7, 'fruit', 'banana'); ?>

Other Examples

If you wanted to make sure that there were no fields with the key "my_key", before adding it:

<?php add_post_meta(68, 'my_key', '47', true); ?>

To add several values to the key "my_key":

<?php add_post_meta(68, 'my_key', '47'); ?>
<?php add_post_meta(68, 'my_key', '682'); ?>
<?php add_post_meta(68, 'my_key', 'The quick, brown fox jumped over the lazy dog.'); ?>
...

For a more detailed example, go to the post_meta Functions Examples page.

Making a "Hidden" Custom Field

If you are a plugin/theme developer and you are planning to use custom fields to store parameters related to your plugin or template, it is interesting to note that WordPress won't show keys starting with an "_" (underscore) in the custom fields list at the page/post editing page. That being said, it is a good practice to use an underscore as the first character in your custom parameters, that way your settings are stored as custom fields, but they won't display in the custom fields list in the admin UI.

The following example:

<?php add_post_meta(68, '_color', 'red', true); ?>

will add a unique custom field with the key name "_color" and the value "red" but this custom field will not display in the page/post editing page.

Related

カスタムフィールド: the_meta(), get_post_meta(), add_post_meta(), update_post_meta(), delete_post_meta(), get_post_custom(), get_post_custom_values(), get_post_custom_keys() (post_meta 関数の例 /en も参照)


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

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