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

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

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

提供: WordPress Codex 日本語版
< 関数リファレンス
2010年4月22日 (木) 20:42時点におけるKz (トーク | 投稿記録)による版 (en:Function Reference/update post meta 19:34, 2 February 2010 Mslade)

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

Description

The function, update post meta(), updates the value of an existing meta key (custom field) for the specified post.

This may be used in place of add_post_meta() function. The first thing this function will do is make sure that $meta_key already exists on $post_id. If it does not, add_post_meta($post_id, $meta_key, $meta_value) is called instead and its results are returned.

True will always be returned.

Usage

%%% <?php update_post_meta($post_id, $meta_key, $meta_value, $prev_value); ?> %%%

Parameters

$post_id
integer) (必須) The ID of the post which contains the field you will edit.
初期値: なし
$meta_key
string) (必須) The key of the custom field you will edit.
初期値: なし
$meta_value
string) (必須) The new value of the custom field.
初期値: なし
$prev_value
string) (optional) The old value of the custom field you wish to change. This is to differentiate between several fields with the same key. If omitted, and there are multiple rows for this post and meta key, all meta values will be updated.
初期値: なし

Examples

Default Usage

<?php update_post_meta(76, 'my_key', 'Steve'); ?>

Other Examples

Assuming a post has an ID of 76, and the following 4 custom fields:

[key_1] => 'Happy'
[key_1] => 'Sad'
[key_2] => 'Gregory'
[my_key] => 'Steve'

To change key_2's value to Hans:

<?php update_post_meta(76, 'key_2', 'Hans'); ?>

To change key_1's value from Sad to Happy:

<?php update_post_meta(76, 'key_1', 'Happy', 'Sad'); ?>

The fields would now look like this:

[key_1] => 'Happy'
[key_1] => 'Happy'
[key_2] => 'Hans'
[my_key] => 'Steve'

Note: This function will update only the first field that matches the criteria.

To change the first key_1's value from Happy to Excited:

<?php 
  update_post_meta(76, 'key_1', 'Excited', 'Happy');

  //Or

  update_post_meta(76, 'key_1', 'Excited');

  //To change all fields with the key "key_1":

  $key1_values = get_post_custom_values('key_1', 76);
  foreach ( $key1_values as $value )
    update_post_meta(76, 'key_1', 'Excited', $value);
?>

For a more detailed example, go to the post_meta Functions Examples 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/update post meta最新版との差分

[[en:Function Reference/update post meta]