从循环PHP外部获取提交的表单的post_id

时间:2018-09-27 18:40:38

标签: php wordpress forms advanced-custom-fields

我正在开发一个应用程序,该应用程序具有将领养导师与受训者匹配的帖子形式。 这位于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);

2 个答案:

答案 0 :(得分:0)

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

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

希望它会起作用。

  

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

答案 1 :(得分:0)

acf/save_post挂钩仅将$post_id作为单个参数传递。您的回调中有3个参数。 https://www.advancedcustomfields.com/resources/acf-save_post/。另外,您需要具有高于10的优先级才能从帖子中获取更新值。

function match_post_title_auto( $post_id ) {
    // do stuff
}
add_filter('acf/save_post', 'match_post_title_auto', 20 );