php - 从循环PHP外部获取提交表单的post_id
问题描述
我正在开发一个应用程序,该应用程序有一个帖子表格,可以将收养导师与学员匹配。这是在 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);
解决方案
您正在尝试通过过滤器挂钩捕获值。实际上你只需要动作钩子和 1 个参数。像这样:
add_action('acf/save_post', 'match_post_title_auto', 10, 1);
希望它会奏效。
有关更多信息,请查看标准文档:
https ://www.advancedcustomfields.com/resources/acf-save_post/
推荐阅读
- java - Java JDBC mysql中的更新查询不起作用
- python - 为什么我可以打印到命令行但不能使用 docker 写入文本文件?
- java - 为什么我自己的 AtomicLong 比 JDK 中提供的要慢?
- javascript - 为什么在条件渲染中解析 Math.mod() 的错误?
- python - 有没有办法在包含字符串的对象数组上使用 numpy 字符串数组函数?
- react-native - 为什么在使用 Firebase 注册用户时,Android 和 IOS 设备模拟器之间存在差异?
- c++ - 在 std::list 中,std::distance(it.begin(), std::prev(it.end())) 是否等于 list.size()?
- javascript - 反应检查为空
- yii2 - 如何修复 Yii2 中的 akiraz2 博客 URL 规则错误
- java - 无法在另一个类中使用接受 Hashmap 作为参数的函数