返回介绍

update_metadata_by_mid()

发布于 2017-09-11 11:04:40 字数 4568 浏览 1026 评论 0 收藏 0

update_metadata_by_mid( string $meta_type,  int $meta_id,  string $meta_value,  string $meta_key = false )

Update meta data by meta ID


description


参数

$meta_type

(string) (Required) Type of object metadata is for (e.g., comment, post, or user)

$meta_id

(int) (Required) ID for a specific meta row

$meta_value

(string) (Required) Metadata value

$meta_key

(string) (Optional) you can provide a meta key to update it

Default value: false


返回值

(bool) True on successful update, false on failure.


源代码

File: wp-includes/meta.php

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 = intval( $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';

	// Fetch the meta and go on if it's found.
	if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
		$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;
		}

		// Sanitize the meta
		$_meta_value = $meta_value;
		$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );
		$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;
}

更新日志

Versiondescription
3.3.0Introduced.

相关函数

Uses

  • wp-includes/cache.php: wp_cache_delete()
  • wp-includes/formatting.php: sanitize_key()
  • wp-includes/functions.php: maybe_serialize()
  • wp-includes/plugin.php: do_action()
  • wp-includes/wp-db.php: wpdb::update()
  • wp-includes/meta.php: _get_meta_table()
  • wp-includes/meta.php: sanitize_meta()
  • wp-includes/meta.php: get_metadata_by_mid()
  • wp-includes/meta.php: update_{$meta_type}_meta
  • wp-includes/meta.php: update_postmeta
  • wp-includes/meta.php: updated_{$meta_type}_meta
  • wp-includes/meta.php: updated_postmeta
  • Show 7 more uses Hide more uses

Used By

  • wp-admin/includes/post.php: update_meta()
  • wp-admin/includes/ajax-actions.php: wp_ajax_add_meta()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::set_custom_fields()

User Contributed Notes

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

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

发布评论

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