Support

Account

Home Forums Front-end Issues Problem to reset conditional fields Reply To: Problem to reset conditional fields

  • I think you’re going to need to check the value of your field that sets the condition for the field you want to display, and then write the PHP to either show or not show the conditional field. This is what I do when building templates.

    
    if (get_field('condition_field')) {
      the_field('conditional_field');
    }
    

    Not sure how you can translate that with the array returned by get_fields(). The test is going to need to be build into the loop you’re using to show your values, probably defeating the purpose using of get_field() since I’m assuming it is because you have a lot of fields to display.

    I don’t use this function because 99% of my field names begin with “_” (underscore) which makes the function pretty useless for me.

    You’re suggestion of deleting the value if the condition is false I think would be difficult. I think the only way to do this would be to find and delete all value before inserting the new values. My suggestion here would be to use the hook “acf/save_post” and in your hooked function use the WP function “delete_post_meta()” to delete all the meta values you want to have cleared. You may also need to scrub through the $_POST array to remove any values that should not be set. Really, it would be easier to use my first suggestion and do a test when displaying them then to do all of the work involved with clearing values before the save.

    I don’t think this is something that ACF should do by default because everyone may not want this to happen. For example, having the values saved and not cleared makes it easy for users to switch back and forth between 2 options without needing to reenter any data. My clients would find that quite annoying.

    I don’t think the JavaScript used in the admin/forms for showing and hiding conditional fields will work without the form or in templates, you may need to write your own JS to do this.