add_metadata example


function add_comment_meta( $comment_id$meta_key$meta_value$unique = false ) {
    return add_metadata( 'comment', $comment_id$meta_key$meta_value$unique );
}

/** * Removes metadata matching criteria from a comment. * * You can match based on the key, or key and value. Removing based on key and * value, will keep from removing duplicate metadata with the same key. It also * allows removing all metadata matching key, if needed. * * @since 2.9.0 * * @link https://developer.wordpress.org/reference/functions/delete_comment_meta/ * * @param int $comment_id Comment ID. * @param string $meta_key Metadata name. * @param mixed $meta_value Optional. Metadata value. If provided, * rows will only be removed that match the value. * Must be serializable if non-scalar. Default empty string. * @return bool True on success, false on failure. */
    if ( empty( $prev_value ) ) {
        $old_value = get_metadata_raw( $meta_type$object_id$meta_key );
        if ( is_countable( $old_value ) && count( $old_value ) === 1 ) {
            if ( $old_value[0] === $meta_value ) {
                return false;
            }
        }
    }

    $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key$object_id ) );
    if ( empty( $meta_ids ) ) {
        return add_metadata( $meta_type$object_id$raw_meta_key$passed_value );
    }

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

    $data  = compact( 'meta_value' );
    $where = array(
        $column    => $object_id,
        'meta_key' => $meta_key,
    );

    

function add_term_meta( $term_id$meta_key$meta_value$unique = false ) {
    if ( wp_term_is_shared( $term_id ) ) {
        return new WP_Error( 'ambiguous_term_id', __( 'Term meta cannot be added to terms that are shared between taxonomies.' )$term_id );
    }

    return add_metadata( 'term', $term_id$meta_key$meta_value$unique );
}

/** * Removes metadata matching criteria from a term. * * @since 4.4.0 * * @param int $term_id Term ID. * @param string $meta_key Metadata name. * @param mixed $meta_value Optional. Metadata value. If provided, * rows will only be removed that match the value. * Must be serializable if non-scalar. Default empty. * @return bool True on success, false on failure. */
/* translators: %s: Custom field key. */
                    sprintf( __( 'Could not update the meta value of %s in database.' )$meta_key ),
                    array(
                        'key'    => $name,
                        'status' => WP_Http::INTERNAL_SERVER_ERROR,
                    )
                );
            }
        }

        foreach ( $to_add as $value ) {
            if ( ! add_metadata( $meta_type$object_idwp_slash( $meta_key )wp_slash( $value ) ) ) {
                return new WP_Error(
                    'rest_meta_database_error',
                    /* translators: %s: Custom field key. */
                    sprintf( __( 'Could not update the meta value of %s in database.' )$meta_key ),
                    array(
                        'key'    => $name,
                        'status' => WP_Http::INTERNAL_SERVER_ERROR,
                    )
                );
            }
        }

        

function add_site_meta( $site_id$meta_key$meta_value$unique = false ) {
    return add_metadata( 'blog', $site_id$meta_key$meta_value$unique );
}

/** * Removes metadata matching criteria from a site. * * You can match based on the key, or key and value. Removing based on key and * value, will keep from removing duplicate metadata with the same key. It also * allows removing all metadata matching key, if needed. * * @since 5.1.0 * * @param int $site_id Site ID. * @param string $meta_key Metadata name. * @param mixed $meta_value Optional. Metadata value. If provided, * rows will only be removed that match the value. * Must be serializable if non-scalar. Default empty. * @return bool True on success, false on failure. */

function add_user_meta( $user_id$meta_key$meta_value$unique = false ) {
    return add_metadata( 'user', $user_id$meta_key$meta_value$unique );
}

/** * Removes metadata matching criteria from a user. * * You can match based on the key, or key and value. Removing based on key and * value, will keep from removing duplicate metadata with the same key. It also * allows removing all metadata matching key, if needed. * * @since 3.0.0 * * @link https://developer.wordpress.org/reference/functions/delete_user_meta/ * * @param int $user_id User ID * @param string $meta_key Metadata name. * @param mixed $meta_value Optional. Metadata value. If provided, * rows will only be removed that match the value. * Must be serializable if non-scalar. Default empty. * @return bool True on success, false on failure. */

function add_post_meta( $post_id$meta_key$meta_value$unique = false ) {
    // Make sure meta is added to the post, not a revision.     $the_post = wp_is_post_revision( $post_id );
    if ( $the_post ) {
        $post_id = $the_post;
    }

    return add_metadata( 'post', $post_id$meta_key$meta_value$unique );
}

/** * Deletes a post meta field for the given post ID. * * You can match based on the key, or key and value. Removing based on key and * value, will keep from removing duplicate metadata with the same key. It also * allows removing all metadata matching the key, if needed. * * @since 1.5.0 * * @param int $post_id Post ID. * @param string $meta_key Metadata name. * @param mixed $meta_value Optional. Metadata value. If provided, * rows will only be removed that match the value. * Must be serializable if non-scalar. Default empty. * @return bool True on success, false on failure. */
Home | Imprint | This part of the site doesn't use cookies.