Random WordPress Function

Learn about a new WordPress function every day!


Function Signature:

update_metadata

Function Description:

Updates metadata for the specified object. If no value already exists for the specified object ID and metadata key, the metadata will be added.

Function Examples:

⚠️ Examples below are generated with GPT-3 once every hour. Do not take them too seriously.
Consider them as some extra input in your learning process - reason about them. Will it work? What could fail?
// Example 1: Updating a post's custom field value
$post_id = 123;
$meta_key = 'price';
$meta_value = 50;

// Check if the post ID exists
if (get_post_status($post_id)) {
    update_metadata('post', $post_id, $meta_key, $meta_value);
} else {
    echo 'Post ID does not exist.';
}
// Example 2: Updating a user's meta field value
$user_id = 456;
$meta_key = 'city';
$meta_value = 'New York';

// Check if the user ID exists
if (get_userdata($user_id)) {
    update_metadata('user', $user_id, $meta_key, $meta_value);
} else {
    echo 'User ID does not exist.';
}
// Example 3: Updating a term's meta field value
$term_id = 789;
$meta_key = 'color';
$meta_value = 'blue';

// Check if the term ID exists
if (term_exists($term_id)) {
    update_metadata('term', $term_id, $meta_key, $meta_value);
} else {
    echo 'Term ID does not exist.';
}