Support

Account

Home Forums Front-end Issues acf_form(): unable to select and save data to group sub fields

Solved

acf_form(): unable to select and save data to group sub fields

  • Hello,

    I wish to capture data in a front end form using acf_form(). I only wish to capture data in a selection of fields including a selection of sub fields of a group.

    For example, I have a group field ‘product’ which has some child fields ‘name’, ‘code’ and ‘intro’.

    I only wish to display and update fields ‘code’ and ‘intro’ via my front enf form.

    To do this I use the following code:

    acf_form(array(
      'post_id' => 'new_post',
      'post_title' => false,
      'post_content' => false,
      'fields' => array(
        'field_5cd2c74962c37', //code
        'field_5cc079392541f', //intro
      ),
      'submit_value' => 'Create Product',
      'new_post' => array(
        'post_type' => 'product',
        'post_status' => 'draft'
      )
    ));

    After submitting the form, the data is not present in the dashboard view and upon inspection of the wp_postmeta table for the post_id, I can see that the meta_key values are ‘code’ and ‘intro’ rather than ‘product_code’ ‘product_intro’ as would be expected as they are sub_fields.

    Is this expected behaviour? I would be very grateful for any pointers here.

    I have tried to submit this post 3 times now but it’s not submitting for some reason and disappears each time. Please remove if duplicate.

    Thanks

  • You are confusing ACF by trying to include only sub fields. When you do this ACF does not know when the form is submitted that the values belong to sub field and thinks they belong in top level fields. So, basically, you can’t do this.

    The way to do this is to include the group and then not show the field you don’t want updated using an acf/prepare_field filter.

    
    // use the field key for sub fields
    add_filter('acf/prepare_field/key=field_XXXXX', 'hide_field_on_front');
    function hide_field_on_front($field) {
      if (!is_admin()) {
        // front end of site, do not show field
        return false;
      }
      return $field;
    }
    
  • perfect. thanks John.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘acf_form(): unable to select and save data to group sub fields’ is closed to new replies.