update_post_meta

函式
update_post_meta ( $post_id, $meta_key, $meta_value, $prev_value = '' )
引數
  • (int) $post_id Post ID.
    Required:
  • (string) $meta_key Metadata key.
    Required:
  • (mixed) $meta_value Metadata value. Must be serializable if non-scalar.
    Required:
  • (mixed) $prev_value Optional. Previous value to check before updating. If specified, only update existing metadata entries with this value. Otherwise, update all entries. Default empty.
    Required:
    Default: (empty)
返回值
  • (int|bool) Meta ID if the key didn't exist, true on successful update, false on failure or if the value passed to the function is the same as the one that is already in the database.
定義位置
相關方法
update_site_metadelete_post_metaupdate_user_metaupdate_metaupdate_term_meta
引入
1.5.0
棄用
-

update_post_meta: 這個函式更新一個文章的後設資料。它需要三個引數:$post_id,$meta_key,和$meta_value。$post_id是需要更新後設資料的文章的ID。$meta_key是要更新的後設資料欄位的名稱。$meta_value是後設資料的新值。

根據給定的文章ID更新一個文章元域。

使用`$prev_value`引數來區分具有相同鍵和文章ID的元欄位。

如果文章的元欄位不存在,它將被新增並返回其ID。

可以用來代替add_post_meta()。”

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) {
// Make sure meta is updated for the post, not for a revision.
$the_post = wp_is_post_revision( $post_id );
if ( $the_post ) {
$post_id = $the_post;
}
return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value );
}
function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) { // Make sure meta is updated for the post, not for a revision. $the_post = wp_is_post_revision( $post_id ); if ( $the_post ) { $post_id = $the_post; } return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value ); }
function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) {
	// Make sure meta is updated for the post, not for a revision.
	$the_post = wp_is_post_revision( $post_id );
	if ( $the_post ) {
		$post_id = $the_post;
	}

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

常見問題

FAQs
檢視更多 >