发布或更新时保存 ACF 字段 post

Save ACF field when publish or update post

当我发布或更新 post 时,我想将 post 的 slug 保存在名为“plan_slug”的 ACF 字段中。 我写的代码是:

add_action('future_to_publish', 'actions_published_post');
add_action('new_to_publish', 'actions_published_post');
add_action('draft_to_publish' ,'actions_published_post');
add_action('auto-draft_to_publish' ,'actions_published_post');

function actions_published_post($post) {
  update_field('plan_slug', $post->post_name);
}

一切顺利! post 后端字段未填满...

你能告诉我我的代码有什么问题吗? 提前谢谢你。

这应该有效

add_action( 'save_post', 'save_custom_field', 10, 2);
function save_custom_field( $post_id, $post ) {
    update_post_meta( $post_id, 'plan_slug', $post->post_name );
}