ACF自定义字段。在functions.php内获取栏内动作
问题描述:
Wordpress + Timber + ACF Pro。在functions.php里面,我有一个行为,每当发布一篇文章(有一周的类型)时就会触发它。ACF自定义字段。在functions.php内获取栏内动作
我想从这篇文章中获取数据,并使用它为每个用户创建一个新帖子。
我有它追求时尚。当一篇文章发表后,我会抓取标题和用户名,并将其用作新创建文章的标题。
但是,在尝试提取ACF数据时遇到问题 - 例如:week_commencing date字段。所有ACF数据都返回NULL(我知道这些字段已填充)。
我已阅读文档 - 访问数据的哪个状态,您需要调用get_field('field_name','post_id') - 我已经完成了。
我写出了$ ID - 所以知道这是正确的。
难道这是由于我运行的东西的顺序吗?
这里是我的代码:
function weekly_published_post_setup($ID, $post) {
$customers = get_users();
$theDate = get_field("week_commencing", $ID);
// Array of WP_User objects.
foreach ($customers as $user) {
$new_post = array(
'post_type' => 'weekly_tasks',
'post_title' => $post->post_title . ' - ' . $theDate . ' - ' . $user->display_name,
'post_content' => $theDate,
'post_status' => 'publish',
'post_author' => $user->ID
);
wp_insert_post($new_post);
}
}
add_action('publish_week', 'weekly_published_post_setup', 10, 2);
**编辑**
事实证明,正在创建的WordPress邮寄的ACF领域之前被保存?所以一个朋友重构我的代码来使用不同的事件。然而,当帖子发布时,这不会被触发...
function week_published_delivery_setup($ID) {
$post = get_post($ID);
if ($post->post_type != 'week') {
return;
}
if($post->post_modified_gmt != $post->post_date_gmt){
return;
}
$customers = get_users();
$field = get_field('week_commencing', $ID);
$fields = post.get_field_objects($ID);
if($fields)
{
foreach($fields as $field_name => $field)
{
$tmp .= $field['label'] . $field['value'];
}
}*/
// Array of WP_User objects.
foreach ($customers as $user) {
$new_delivery_post = array(
'post_type' => 'delivery',
'post_title' => $post->post_title . ' - ' . $field . ' - ' . $user->display_name,
'post_content' => $post->post_title,
'post_status' => 'publish',
'post_author' => $user->ID
);
wp_insert_post($new_delivery_post);
}
}
add_action('acf/save_post', 'week_published_delivery_setup', 20);
答
因此,现在测试发布时的发布状态 - 然后执行所需的任务。触发动作,如果old_status == future和new_status == publish似乎有诀窍。
function on_all_status_transitions($new_status, $old_status, $post) {
$ID = $post->ID;
if ($post->post_type != 'week') {
return;
}
if ($new_status != $old_status && $old_status == 'future' && $new_status == 'publish') {
$customers = get_users();
$field = get_field('week_commencing', $ID);
// Array of WP_User objects.
foreach ($customers as $user) {
$new_delivery_post = array(
'post_type' => 'delivery',
'post_title' => $post->post_title . ' - ' . $field . ' - ' . $user->display_name,
'post_content' => $post->post_title,
'post_status' => 'publish',
'post_author' => $user->ID
);
wp_insert_post($new_delivery_post);
}
}
}
add_action( 'transition_post_status', 'on_all_status_transitions', 20, 3);