Support

Account

Home Forums Bug Reports Hidden fields when editing a post are not saved

Solving

Hidden fields when editing a post are not saved

    • bonbouaz

    • September 16, 2019 at 6:54 pm

    Hello, here is the bug :
    I have two fields #field1 and #field2
    #field2 is hidden unless #field1’s value is “B”

    1. I create a new post, with #field1=”B”, #field2 then appears and I type in #field2=”something”, save the post and everything’s fine.
    2. I edit this post and change the value of #field1=”A”. #field2 then disappears and when I save the post, only #field1’s value is saved.
    3. if I try to echo get_field(“#field2”); I still get the “something” I typed in in the first place, whereas I would like to have an empty field.
    4. How should I resolve this problem ?

      PS : I tried to use jQuery to empty every hidden fields prior to save the form, but it’s not working either

    • bonbouaz

    • September 16, 2019 at 6:59 pm

    Another way to reproduce this bug is to enter #field2=”somethingelse” prior to type #field1=”A”

    The value “somethingelse” won’t be passed by the form since #field2 has disappeared

  • This is not a bug, but the intended operation.

    When a field is conditional and it does not appear, that fields value is not changed. It is not changed because the field in not submitted.

    ACF does not look at field 1 to see if the conditions are met when saving field 2. The condition is on field 2. ACF has no idea when a field is save what other fields might be conditional base on the value of the field being saved.

    In your template code you need to do the work something like this.

    
    if (get_field('field#1') == 'A) {
      echo get_field('field#2');
    }
    

    There have been many discussions on these forums about this. Some like this, some do not. With the way it operates, if a user changes the value and saves the post, if they should change their mind later then the value entered into field 2 will not need to be re-entered.

    The only way around this is to create an acf/save_post filter https://www.advancedcustomfields.com/resources/acf-save_post/

    
    add_filter('acf/save_post', 'my_save_post_function_name');
    function my_save_post_function_name($post_id) {
      if (get_field('field#1', $post_id) != 'A') {
        delete_field('field#2', $post_id);
      }
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.