将元数据字段添加到帖子。
原型
add_post_meta( int $post_id, string $meta_key, mixed $meta_value, bool $unique = false )
描述
发布元数据在管理屏幕上称为“自定义字段”。
参数
$post_id
(int)
(Required)
帖子ID。
$meta_key
(string)
(Required)
元数据名称。
$meta_value
(mixed)
(Required)
元数据价值。如果是非标量,则必须可序列化。
$unique
(bool)
(Optional)
是否不应添加相同的密钥。
返回值
(int|false)
成功时的元ID,失败时的假。
源文件
路径:wp-includes/post.php
<?php
...
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 );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/add_post_meta/