Wordpress 在function.php上更新post meta不起作用

Wordpress 在function.php上更新post meta不起作用,wordpress,Wordpress,当帖子保存时,我正在尝试更新帖子元 我希望在保存名为“2a”的自定义字段时,只在其中添加一次发布日期。没有通过修改post更新“2a” 这是我在function.php上添加的代码。它不影响自定义字段上的任何内容。我的密码有错误吗 当我转储它时,它返回我期望的正确值。但是,它不会更新后元 function add_custom_field( $post_id, $post, $update ) { if ( is_single() && metadata_exists( 'pos

当帖子保存时,我正在尝试更新帖子元

我希望在保存名为“2a”的自定义字段时,只在其中添加一次发布日期。没有通过修改post更新“2a”

这是我在function.php上添加的代码。它不影响自定义字段上的任何内容。我的密码有错误吗

当我转储它时,它返回我期望的正确值。但是,它不会更新后元

function add_custom_field( $post_id, $post, $update ) {
if ( is_single() && metadata_exists( 'post', $post_id, '2a' ) ) :{
    $date = get_post_meta( $post_id, '2a', true );
    update_post_meta($post_id, '2a', $date );}
    else:{
    $date = get_the_date('Y-m-d', $post_id);    
     return update_metadata('post', $post_id, '2a', $date,); 
    }
endif;
}

add_action( 'save_post', 'add_custom_field', 10, 3 );
我发现即使是这个简单的代码也不起作用。。。为什么

function update_custom_field( $post_id, $post, $update ) {
    $date = get_the_date('Y-m-d', $post_id);    
    update_post_meta($post_id, '2a', $date);
}
add_action( 'save_post', 'update_custom_field', 10, 3 );

点击这里-谢谢你的评论。获取post-meta,如果某个meta-empty是实现此目标的方法之一,则触发它,但如果可能,我更希望元数据存在。无论如何,谢谢你。