【发布时间】:2018-09-27 18:40:37
【问题描述】:
我正在开发一个应用程序,该应用程序有一个帖子表单,可以将收养导师与受指导者进行匹配。 这是在 Wordpress/PHP/MySQL/ACF(高级自定义字段)中。
提交表单后,我无法获取该帖子的 post_id,因此我可以保存列出所有匹配项的屏幕的 ACF 字段“标题”。
当我处于“循环之外”时,是否需要检索该表单的$post_id?我该怎么做?
function match_post_title_auto( $post_id ) {
// get mentor & new mentee user array
$mentee = get_field('match_mentee', $post_id);
$mentor = get_field('match_mentor', $post_id);
$title = ' Mentor: ' . $mentor['display_name'] . ' and Mentee: ' . $mentee['display_name'];
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'match'
);
wp_update_post( $postdata );
return $value;
}
var_dump($post_id); //returns NULL
//what do I put here to get that `post_id`? Thanks!
add_action('acf/save_post', 'match_post_title_auto', 10, 1);
【问题讨论】:
-
不清楚您要做什么。你说的是什么形式?照原样,您的函数看起来会在保存时更改任何具有自定义字段的帖子的标题,但听起来这不是您想要做的。此外,当过滤器只接受一个 $post_id 时,您将三个参数传递给您的函数
-
您的“var_dump($post_id);//returns NULL”预计无法正常工作。您正在函数范围之外访问 $post_id 。打开一个 wordpress 调试选项codex.wordpress.org/Debugging_in_WordPress 并使用 error_log() 对函数 match_post_title_auto 中的代码进行故障排除。
标签: php wordpress forms advanced-custom-fields