返回介绍

update_post_meta()

发布于 2017-09-11 11:05:46 字数 6286 浏览 1451 评论 0 收藏 0

update_post_meta( int $post_id,  string $meta_key,  mixed $meta_value,  mixed $prev_value = '' )

Update post meta field based on post ID.


description

Use the $prev_value parameter to differentiate between meta fields with the same key and post ID.

If the meta field for the post does not exist, it will be added.


参数

$post_id

(int) (Required) Post ID.

$meta_key

(string) (Required) Metadata key.

$meta_value

(mixed) (Required) Metadata value. Must be serializable if non-scalar.

$prev_value

(mixed) (Optional) Previous value to check before removing.

Default value: ''


返回值

(int|bool) Meta ID if the key didn't exist, true on successful update, false on failure.


源代码

File: wp-includes/post.php

function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) {
	// Make sure meta is added to the post, not a revision.
	if ( $the_post = wp_is_post_revision($post_id) )
		$post_id = $the_post;

	return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value);
}

更新日志

Versiondescription
1.5.0Introduced.

相关函数

Uses

  • wp-includes/revision.php: wp_is_post_revision()
  • wp-includes/meta.php: update_metadata()

Used By

  • wp-includes/class-wp-customize-manager.php: WP_Customize_Manager::import_theme_starter_content()
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::create_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php: WP_REST_Attachments_Controller::update_item()
  • wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php: WP_REST_Posts_Controller::handle_template()
  • wp-admin/custom-background.php: Custom_Background::ajax_background_add()
  • wp-admin/includes/image-edit.php: wp_restore_image()
  • wp-admin/includes/image-edit.php: wp_save_image()
  • wp-admin/includes/image.php: wp_generate_attachment_metadata()
  • wp-admin/includes/media.php: media_upload_form_handler()
  • wp-admin/includes/post.php: wp_set_post_lock()
  • wp-admin/includes/post.php: edit_post()
  • wp-admin/includes/ajax-actions.php: wp_ajax_upload_attachment()
  • wp-admin/includes/ajax-actions.php: wp_ajax_wp_remove_post_lock()
  • wp-admin/includes/ajax-actions.php: wp_ajax_save_attachment()
  • wp-admin/custom-header.php: Custom_Image_Header::ajax_header_add()
  • wp-admin/custom-header.php: Custom_Image_Header::customize_set_last_used()
  • wp-admin/custom-header.php: Custom_Image_Header::set_header_image()
  • wp-admin/custom-background.php: Custom_Background::wp_set_background_image()
  • wp-admin/custom-background.php: Custom_Background::handle_upload()
  • wp-includes/class-wp-embed.php: WP_Embed::shortcode()
  • wp-includes/post.php: set_post_thumbnail()
  • wp-includes/post.php: wp_update_attachment_metadata()
  • wp-includes/post.php: wp_insert_post()
  • wp-includes/post.php: update_attached_file()
  • wp-includes/revision.php: wp_restore_post_revision()
  • wp-includes/nav-menu.php: wp_update_nav_menu_item()
  • Show 21 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by Fiaz Husyn

    Note: if there are multiple values for a single key, all the values will get updated if $prev_value is not set.

  2. Default Usage

    
    <?php update_post_meta( $post_id = 76, $key = 'my_key', $value = '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 Excoded:

    
    <?php 
    update_post_meta( 76, 'key_1', 'Excoded', 'Happy' );
    
    //Or
    
    update_post_meta( 76, 'key_1', 'Excoded' );
    
    //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', 'Excoded', $value );
    ?>
    

    Edit Page template

    [/php]

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文