Support

Account

Home Forums Bug Reports Conditional fields data stored when condition not met

Helping

Conditional fields data stored when condition not met

    • netfuel

    • June 17, 2020 at 11:18 pm

    Hello,
    I have worked on several projects where I have set a field using conditional to detect whether another field is set. If the condition is no longer met the data is still stored which breaks the frontend.

    For example I’ve got a checkbox field called “Show Price”, and another field (“Price”) which shows conditionally if the checkbox is ticked. If the checkbox is unticked at a later date, the data for price is retained so still shows on the frontend.

    Would it be possible to implement something so that data previously conditionally met is removed if the condition no longer applies? Or even add an option in settings that allows admin to ensure this?

    Kind regards
    Liam

  • There isn’t a setting to do this and this topic has come up in the past. When the field is hidden by conditional logic it is not submitted. Anything that is not submitted is not updated. This is a JS only thing and does not effect the fields. When fields are being saved ACF is unaware of the conditional logic associated with each field……

    Long story short: If you want to do this then you need to add your own filters/actions to accomplish it. acf/save_post https://www.advancedcustomfields.com/resources/acf-save_post/

    When building templates you should not be basing display of the field on the content of the conditional field but on the condition field.

    Let’s say that I have 2 fields “show_price” and another field “price”

    
    if (get_field('show_price')) {
      the_field('price');
    }
    
Viewing 2 posts - 1 through 2 (of 2 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.