update_post_meta() - WordPress 函数参考中文文档

返回介绍

update_post_meta()

发布于 2017-09-11 字数6288 浏览 965 评论 0

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

Update post meta field based on post ID.


描述

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);
}

更新日志

Version 描述
1.5.0 Introduced.

相关函数

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.

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!