首页 > 解决方案 > 从循环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);

标签: phpwordpressformsadvanced-custom-fields

解决方案


您正在尝试通过过滤器挂钩捕获值。实际上你只需要动作钩子和 1 个参数。像这样:

add_action('acf/save_post', 'match_post_title_auto', 10, 1);

希望它会奏效。

有关更多信息,请查看标准文档:
https ://www.advancedcustomfields.com/resources/acf-save_post/


推荐阅读