update_post_meta()更新文章/页面的自定义字段(post meta)信息

目录

描述

译文

该函数为指定文章更新已有meta关键字(自定义)的值。

成功更新后函数返回true,若文章未指定meta关键字则返回false。

可用add_post_meta()函数添加新的meta关键字和值。

原文

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 result is returned.

Returns meta_id if the meta doesn't exist, otherwise returns true on success and false on failure. It also returns false if the value submitted is the same as the value that is already in the database.

Please note that if your database collation is case insensitive (has with suffix _ci) then update_post_meta and delete_post_meta and get_posts will update/delete/query the meta records with keys that are upper or lower case. However get_post_meta will apparently be case sensitive due to WordPress caching. See https://core.trac.wordpress.org/ticket/18210 for more info.

update_post_meta() 描述

用法

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

update_post_meta() 用法

参数

$post_id

(integer) (必填) The ID of the post which contains the field you will edit.

默认值: None

$meta_key

(string) (必填) The key of the custom field you will edit. (this should be raw as opposed to sanitized for database queries)

默认值: None

$meta_value

(mixed) (必填) The new value of the custom field. A passed array will be serialized into a string.(this should be raw as opposed to sanitized for database queries)

默认值: None

$prev_value

(mixed) (可选) 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.

默认值: Empty

update_post_meta() 参数

返回值

(mixed) 

Returns meta_id if the meta doesn't exist, otherwise returns true on success and false on failure. NOTE: If the meta_value passed to this function is the same as the value that is already in the database, this function returns false.

update_post_meta() 返回值

示例

Default Usage

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:

To change key_1's value from Sad to Happy:

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:

Edit Page template

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

update_post_meta() 示例

注意

update_post_meta() 注意

历史

添加于 版本 1.5.0

update_post_meta() 历史

相关