Wordpress 如何在保存 post 后更新自定义 post 类型中的 ACF 字段
Wordpress how to update ACF field in a custom post type after the post has been saved
我正在尝试在创建 post 之后立即设置 custom post type
中的字段值。这是我的代码:
add_action('acf/save_post', 'set_coach_email');
function set_coach_email( $post_id ){
$posttype = get_post_type($post_id);
if ('team' !== $posttype){
return;
}
$email = 'test@test.com';
update_field('coach_email', $email, $post_id);
}
我使用 ACF fields
创建了这个自定义 post 类型,但我似乎无法让它工作。
我会检查相反的条件检查。另外我会先检查该字段是否为空,然后我只会 运行 如果该字段为空则更新。
add_action('acf/save_post', 'set_coach_email');
function set_coach_email($post_id)
{
$posttype = get_post_type($post_id);
$email_field = get_field('coach_email', $post_id);
if ('team' == $posttype && empty($email_field)) {
$email = 'coachtest@test.com';
update_field('coach_email', $email, $post_id);
}
}
刚刚在我自己的自定义 post 类型上进行了测试,效果很好。让我知道你是否也能让它工作!
我发现有时使用 acf/save_post
,提高优先级可确保在 运行 执行操作函数之前其他所有内容都具有 运行。
这可能在 get_field()
函数中传递 $post_id
时发挥作用,我倾向于在使用 acf/save_post
时不传递 $post_id
以确保当前使用最新的现场数据。但这个理论可能并非如此。请参阅下面代码中的注释...
<?php
// save post action with priority 20 (default 10)
add_action('acf/save_post', 'set_coach_email', 20);
/**
* @param $post_id int|string
*/
function set_coach_email($post_id) {
// get our current post object
$post = get_post($post_id);
// if post is object
if(is_object($post)) {
// check we are on the team custom type and post status is either publish or draft
if($post->post_type === 'team' && ($post->post_status === 'publish' || $post->post_status === 'draft')) {
// get coach email field
$coach_email = get_field('coach_email');
// if coach email field returns false
if(!$coach_email) {
// coach email default
$email = 'coachtest@test.com';
// update coach email field
update_field('coach_email', $email, $post->ID);
}
}
}
// finally return
return;
}
我正在尝试在创建 post 之后立即设置 custom post type
中的字段值。这是我的代码:
add_action('acf/save_post', 'set_coach_email');
function set_coach_email( $post_id ){
$posttype = get_post_type($post_id);
if ('team' !== $posttype){
return;
}
$email = 'test@test.com';
update_field('coach_email', $email, $post_id);
}
我使用 ACF fields
创建了这个自定义 post 类型,但我似乎无法让它工作。
我会检查相反的条件检查。另外我会先检查该字段是否为空,然后我只会 运行 如果该字段为空则更新。
add_action('acf/save_post', 'set_coach_email');
function set_coach_email($post_id)
{
$posttype = get_post_type($post_id);
$email_field = get_field('coach_email', $post_id);
if ('team' == $posttype && empty($email_field)) {
$email = 'coachtest@test.com';
update_field('coach_email', $email, $post_id);
}
}
刚刚在我自己的自定义 post 类型上进行了测试,效果很好。让我知道你是否也能让它工作!
我发现有时使用 acf/save_post
,提高优先级可确保在 运行 执行操作函数之前其他所有内容都具有 运行。
这可能在 get_field()
函数中传递 $post_id
时发挥作用,我倾向于在使用 acf/save_post
时不传递 $post_id
以确保当前使用最新的现场数据。但这个理论可能并非如此。请参阅下面代码中的注释...
<?php
// save post action with priority 20 (default 10)
add_action('acf/save_post', 'set_coach_email', 20);
/**
* @param $post_id int|string
*/
function set_coach_email($post_id) {
// get our current post object
$post = get_post($post_id);
// if post is object
if(is_object($post)) {
// check we are on the team custom type and post status is either publish or draft
if($post->post_type === 'team' && ($post->post_status === 'publish' || $post->post_status === 'draft')) {
// get coach email field
$coach_email = get_field('coach_email');
// if coach email field returns false
if(!$coach_email) {
// coach email default
$email = 'coachtest@test.com';
// update coach email field
update_field('coach_email', $email, $post->ID);
}
}
}
// finally return
return;
}