匹配$ _POST键和ACF字段名(Wordpress)

时间:2019-04-05 02:13:52

标签: php wordpress advanced-custom-fields

我只是想知道是否有人知道将通过$ _POST提交的输入名称与acf字段名称相匹配的可能方法。

我正在通过wp_insert_posts()从前端表单在Wordpress CPT中创建帖子。前端表单中的所有字段都需要更新为单独的acf字段。

我正在尝试使过程自动化,而不是为40多个字段编写update_field()。

<input type="text" name="user_name" value="" placeholder="User Name" />
<input type="text" name="userquestion" value="" placeholder="User Question" />

Acf Fields

1 个答案:

答案 0 :(得分:0)

对于以后遇到此问题的任何人,这是我的解决方案。

$acf_fields = array(
    'user_name'          => 'field_5ca80efacb9fb',
    'userQuestion'       => 'field_5ca80f19cb9fc',
    'userQuestionAnswer' => 'field_5ca80f2acb9fd',
);

$new_post = array(
    'post_title'    => $_POST['user_name'],
    'post_status'   => 'publish',          
    'post_type'     =>  'entries' 
);

$post_id = wp_insert_post($new_post);

foreach ( $_POST as $field_name => $value ) {

    if( isset( $_POST[$field_name] ) ) {
        update_field( $acf_fields[$field_name], $value, $post_id );
    }
}
相关问题