Home › Forums › Bug Reports › Hidden fields when editing a post are not saved
Hello, here is the bug :
I have two fields #field1 and #field2
#field2 is hidden unless #field1’s value is “B”
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
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);
}
}
The topic ‘Hidden fields when editing a post are not saved’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.