On this page
add_metadata( string $meta_type, int $object_id, string $meta_key, mixed $meta_value, bool $unique = false ): int|false
Adds metadata for the specified object.
Parameters
$meta_typestring Required-
Type of object metadata is for. Accepts
'post','comment','term','user', or any other object type with an associated meta table. $object_idint Required-
ID of the object metadata is for.
$meta_keystring Required-
Metadata key.
$meta_valuemixed Required-
Metadata value. Must be serializable if non-scalar.
$uniquebool 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:
false
Return
int|false The meta ID on success, false on failure.
Source
File: wp-includes/meta.php. View all references
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;
}
$meta_subtype = get_object_subtype( $meta_type, $object_id );
$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, $meta_subtype );
/**
* Short-circuits adding metadata of a specific type.
*
* The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
* (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:
*
* - `add_post_metadata`
* - `add_comment_metadata`
* - `add_term_metadata`
* - `add_user_metadata`
*
* @since 3.1.0
*
* @param null|bool $check Whether to allow adding metadata for the given type.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar.
* @param bool $unique Whether the specified meta key should be unique for the object.
*/
$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 name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
*
* Possible hook names include:
*
* - `add_post_meta`
* - `add_comment_meta`
* - `add_term_meta`
* - `add_user_meta`
*
* @since 3.1.0
*
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata 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 name, `$meta_type`, refers to the meta object type
* (post, comment, term, user, or any other type with an associated meta table).
*
* Possible hook names include:
*
* - `added_post_meta`
* - `added_comment_meta`
* - `added_term_meta`
* - `added_user_meta`
*
* @since 2.9.0
*
* @param int $mid The meta ID after successful update.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata value.
*/
do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );
return $mid;
}
Hooks
- do_action( "added_{$meta_type}_meta",
int $mid ,int $object_id ,string $meta_key ,mixed $_meta_value ) -
Fires immediately after meta of a specific type is added.
- do_action( "add_{$meta_type}_meta",
int $object_id ,string $meta_key ,mixed $_meta_value ) -
Fires immediately before meta of a specific type is added.
- apply_filters( "add_{$meta_type}_metadata",
null|bool $check ,int $object_id ,string $meta_key ,mixed $meta_value ,bool $unique ) -
Short-circuits adding metadata of a specific type.
Related
Uses
| Uses | Description |
|---|---|
| get_object_subtype() wp-includes/meta.php | Returns the object subtype for a given object ID of a specific type. |
| wp_cache_delete() wp-includes/cache.php | Removes the cache contents matching key and group. |
| maybe_serialize() wp-includes/functions.php | Serializes data, if needed. |
| wpdb::insert() wp-includes/class-wpdb.php | Inserts a row into the table. |
| _get_meta_table() wp-includes/meta.php | Retrieves the name of the metadata table for the specified object type. |
| sanitize_meta() wp-includes/meta.php | Sanitizes meta value. |
| wp_unslash() wp-includes/formatting.php | Removes slashes from a string or recursively removes slashes from strings within an array. |
| sanitize_key() wp-includes/formatting.php | Sanitizes a string key. |
| absint() wp-includes/functions.php | Converts a value to non-negative integer. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
| wpdb::get_var() wp-includes/class-wpdb.php | Retrieves one variable from the database. |
| wpdb::prepare() wp-includes/class-wpdb.php | Prepares a SQL query for safe execution. |
Used By
| Used By | Description |
|---|---|
| add_site_meta() wp-includes/ms-site.php | Adds metadata to a site. |
| WP_REST_Meta_Fields::update_multi_meta_value() wp-includes/rest-api/fields/class-wp-rest-meta-fields.php | Updates multiple meta values for an object. |
| add_term_meta() wp-includes/taxonomy.php | Adds metadata to a term. |
| add_user_meta() wp-includes/user.php | Adds meta data to a user. |
| add_post_meta() wp-includes/post.php | Adds a meta field to the given post. |
| add_comment_meta() wp-includes/comment.php | Adds meta data field to a comment. |
| update_metadata() wp-includes/meta.php | Updates metadata for the specified object. If no value already exists for the specified object ID and metadata key, the metadata will be added. |
Changelog
| Version | Description |
|---|---|
| 2.9.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/add_metadata