返回介绍

add_metadata()

发布于 2017-09-10 20:03:08 字数 5855 浏览 1349 评论 0 收藏 0

add_metadata( string $meta_type,  int $object_id,  string $meta_key,  mixed $meta_value,  bool $unique = false )

Add metadata for the specified object.


description


参数

$meta_type

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

$object_id

(int) (Required) ID of the object metadata is for

$meta_key

(string) (Required) Metadata key

$meta_value

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

$unique

(bool) (Optional) Whether the specified metadata key should be unique for the object. If true, and the object already has a value for the specified metadata key, no change will be made.

Default value: false


返回值

(int|false) The meta ID on success, false on failure.


源代码

File: wp-includes/meta.php

function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) {
	global $wpdb;

	if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
		return false;
	}

	$object_id = absint( $object_id );
	if ( ! $object_id ) {
		return false;
	}

	$table = _get_meta_table( $meta_type );
	if ( ! $table ) {
		return false;
	}

	$column = sanitize_key($meta_type . '_id');

	// expected_slashed ($meta_key)
	$meta_key = wp_unslash($meta_key);
	$meta_value = wp_unslash($meta_value);
	$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );

	/**
	 * Filters whether to add metadata of a specific type.
	 *
	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
	 * object type (comment, post, or user). Returning a non-null value
	 * will effectively short-circuit the function.
	 *
	 * @since 3.1.0
	 *
	 * @param null|bool $check      Whether to allow adding metadata for the given type.
	 * @param int       $object_id  Object ID.
	 * @param string    $meta_key   Meta key.
	 * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
	 * @param bool      $unique     Whether the specified meta key should be unique
	 *                              for the object. Optional. Default false.
	 */
	$check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique );
	if ( null !== $check )
		return $check;

	if ( $unique && $wpdb->get_var( $wpdb->prepare(
		"SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",
		$meta_key, $object_id ) ) )
		return false;

	$_meta_value = $meta_value;
	$meta_value = maybe_serialize( $meta_value );

	/**
	 * Fires immediately before meta of a specific type is added.
	 *
	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
	 * object type (comment, post, or user).
	 *
	 * @since 3.1.0
	 *
	 * @param int    $object_id  Object ID.
	 * @param string $meta_key   Meta key.
	 * @param mixed  $meta_value Meta value.
	 */
	do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );

	$result = $wpdb->insert( $table, array(
		$column => $object_id,
		'meta_key' => $meta_key,
		'meta_value' => $meta_value
	) );

	if ( ! $result )
		return false;

	$mid = (int) $wpdb->insert_id;

	wp_cache_delete($object_id, $meta_type . '_meta');

	/**
	 * Fires immediately after meta of a specific type is added.
	 *
	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
	 * object type (comment, post, or user).
	 *
	 * @since 2.9.0
	 *
	 * @param int    $mid        The meta ID after successful update.
	 * @param int    $object_id  Object ID.
	 * @param string $meta_key   Meta key.
	 * @param mixed  $meta_value Meta value.
	 */
	do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );

	return $mid;
}

更新日志

Versiondescription
2.9.0Introduced.

相关函数

Uses

  • wp-includes/cache.php: wp_cache_delete()
  • wp-includes/formatting.php: wp_unslash()
  • wp-includes/formatting.php: sanitize_key()
  • wp-includes/functions.php: absint()
  • wp-includes/functions.php: maybe_serialize()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/plugin.php: do_action()
  • wp-includes/wp-db.php: wpdb::get_var()
  • wp-includes/wp-db.php: wpdb::insert()
  • wp-includes/wp-db.php: wpdb::prepare()
  • wp-includes/meta.php: _get_meta_table()
  • wp-includes/meta.php: sanitize_meta()
  • wp-includes/meta.php: add_{$meta_type}_metadata
  • wp-includes/meta.php: add_{$meta_type}_meta
  • wp-includes/meta.php: added_{$meta_type}_meta
  • Show 10 more uses Hide more uses

Used By

  • wp-includes/rest-api/fields/class-wp-rest-meta-fields.php: WP_REST_Meta_Fields::update_multi_meta_value()
  • wp-includes/taxonomy.php: add_term_meta()
  • wp-includes/user.php: add_user_meta()
  • wp-includes/post.php: add_post_meta()
  • wp-includes/comment.php: add_comment_meta()
  • wp-includes/meta.php: update_metadata()
  • Show 1 more used by Hide more used by

User Contributed Notes

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

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

发布评论

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