update_metadata_by_mid()
云策文档标注
概述
update_metadata_by_mid() 函数用于通过元数据 ID 更新元数据,支持多种对象类型如 'post'、'user' 等。它验证参数、执行数据库更新,并触发相关 Hook 以支持扩展。
关键要点
- 函数参数包括 $meta_type(对象类型,如 'post')、$meta_id(元数据行 ID)、$meta_value(元数据值,非标量需可序列化)和可选的 $meta_key(用于更新元键)。
- 返回布尔值:成功更新返回 true,失败返回 false。
- 内部流程包括参数验证、元数据检索、键值处理、数据清理(使用 sanitize_meta() 和 maybe_serialize())和数据库更新(通过 $wpdb->update())。
- 更新前后会触发 Hook,如 update_{$meta_type}_meta 和 updated_{$meta_type}_meta,并清除缓存(wp_cache_delete())。
- 相关函数包括 get_metadata_by_mid()、_get_meta_table() 和 sanitize_meta(),用于辅助操作。
代码示例
// 示例:更新 ID 为 123 的 post 元数据
$result = update_metadata_by_mid( 'post', 123, '新值', 'custom_key' );
if ( $result ) {
echo '更新成功';
} else {
echo '更新失败';
}注意事项
- 确保 $meta_type 有效且 $meta_id 为正整数,否则函数返回 false。
- 如果 $meta_key 为 false,则使用原始元键;否则需为字符串类型。
- 非标量 $meta_value 必须可序列化,函数会自动处理序列化。
- 更新操作会触发相关 Hook,开发者可利用这些 Hook 进行自定义处理。
原文内容
Updates metadata by meta ID.
Parameters
$meta_typestringrequired-
Type of object metadata is for. Accepts
'blog','post','comment','term','user', or any other object type with an associated meta table. $meta_idintrequired-
ID for a specific meta row.
$meta_valuestringrequired-
Metadata value. Must be serializable if non-scalar.
$meta_keystring|falseoptional-
You can provide a meta key to update it.
Default:
false
Source
function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
global $wpdb;
// Make sure everything is valid.
if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
return false;
}
$meta_id = (int) $meta_id;
if ( $meta_id <= 0 ) {
return false;
}
$table = _get_meta_table( $meta_type );
if ( ! $table ) {
return false;
}
$column = sanitize_key( $meta_type . '_id' );
$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
/**
* Short-circuits updating metadata of a specific type by meta ID.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (blog, post, comment, term, user, or any other type with an associated meta table).
* Returning a non-null value will effectively short-circuit the function.
*
* Possible hook names include:
*
* - `update_blog_metadata_by_mid`
* - `update_post_metadata_by_mid`
* - `update_comment_metadata_by_mid`
* - `update_term_metadata_by_mid`
* - `update_user_metadata_by_mid`
*
* @since 5.0.0
*
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param int $meta_id Meta ID.
* @param mixed $meta_value Meta value. Must be serializable if non-scalar.
* @param string|false $meta_key Meta key, if provided.
*/
$check = apply_filters( "update_{$meta_type}_metadata_by_mid", null, $meta_id, $meta_value, $meta_key );
if ( null !== $check ) {
return (bool) $check;
}
// Fetch the meta and go on if it's found.
$meta = get_metadata_by_mid( $meta_type, $meta_id );
if ( $meta ) {
$original_key = $meta->meta_key;
$object_id = $meta->{$column};
/*
* If a new meta_key (last parameter) was specified, change the meta key,
* otherwise use the original key in the update statement.
*/
if ( false === $meta_key ) {
$meta_key = $original_key;
} elseif ( ! is_string( $meta_key ) ) {
return false;
}
$meta_subtype = get_object_subtype( $meta_type, $object_id );
// Sanitize the meta.
$_meta_value = $meta_value;
$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
$meta_value = maybe_serialize( $meta_value );
// Format the data query arguments.
$data = array(
'meta_key' => $meta_key,
'meta_value' => $meta_value,
);
// Format the where query arguments.
$where = array();
$where[ $id_column ] = $meta_id;
/** This action is documented in wp-includes/meta.php */
do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
if ( 'post' === $meta_type ) {
/** This action is documented in wp-includes/meta.php */
do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
}
// Run the update query, all fields in $data are %s, $where is a %d.
$result = $wpdb->update( $table, $data, $where, '%s', '%d' );
if ( ! $result ) {
return false;
}
// Clear the caches.
wp_cache_delete( $object_id, $meta_type . '_meta' );
/** This action is documented in wp-includes/meta.php */
do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
if ( 'post' === $meta_type ) {
/** This action is documented in wp-includes/meta.php */
do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
}
return true;
}
// And if the meta was not found.
return false;
}
Hooks
- do_action( ‘updated_postmeta’, int $meta_id, int $object_id, string $meta_key, mixed $meta_value )
-
Fires immediately after updating a post’s metadata.
- do_action( “updated_{$meta_type}_meta”, int $meta_id, int $object_id, string $meta_key, mixed $_meta_value )
-
Fires immediately after updating metadata of a specific type.
- do_action( ‘update_postmeta’, int $meta_id, int $object_id, string $meta_key, mixed $meta_value )
-
Fires immediately before updating a post’s metadata.
- do_action( “update_{$meta_type}_meta”, int $meta_id, int $object_id, string $meta_key, mixed $_meta_value )
-
Fires immediately before updating metadata of a specific type.
- apply_filters( “update_{$meta_type}_metadata_by_mid”, null|bool $check, int $meta_id, mixed $meta_value, string|false $meta_key )
-
Short-circuits updating metadata of a specific type by meta ID.
Changelog
| Version | Description |
|---|---|
| 3.3.0 | Introduced. |