【问题标题】:Matching $_POST key and ACF field name (Wordpress)匹配 $_POST 键和 ACF 字段名称 (Wordpress)
【发布时间】:2019-08-26 21:12:19
【问题描述】:

我只是想知道是否有人知道一种可能的方法来匹配通过 $_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" />

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

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

    $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 );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-02-16
      • 2017-07-27
      • 1970-01-01
      • 2017-06-22
      • 2020-04-04
      • 2019-03-30
      • 1970-01-01
      • 2017-06-20
      • 2017-02-02
      相关资源
      最近更新 更多