Support

Account

Home Forums Backend Issues (wp-admin) Field conditions based on post's properties

Helping

Field conditions based on post's properties

  • Hi,

    I know there’s an option to create custom conditions like: https://www.advancedcustomfields.com/resources/javascript-api/#acf.condition-extend

    But these conditions work based on fields within the currently edited field group, so if one field has value of something then show / hide current field.

    I’m looking for a solution that would enable to write conditions based on current post properties like: if a post has assigned taxonomy of slug equal to something then show / hide this field.

    Can this be solved similarly simple as the acf.condition-extend or would it need much more work to accomplish?

  • It would need more work to accomplish.

    The easiest solution would be to

    1. Create an ACF field that this field can be conditional on, for example a radio field that has all the possible values in it.
    2. Set your conditional fields
    3. Create an acf/prepeare_field filter that sets the value of this radio field base
    4. Use custom CSS to hide this field on the edit page so that users cannot change the values.

    If the page property is something that can be edited, for example based on the a taxonomy term, then you would need to add custom JavaScript that detects that WP field being changed and then updates the value of the field that is hidden by CSS and then

    
    $('your field selector').trigger('change');
    

    More complicated, but possible would be to build your own conditional logic script that detects this the change and then manipulates the class of the field you want to be conditional, this requires adding or removing the class ‘hidden-by-conditional-logic’ to the field container.

    Overall, the easiest way is to have multiple field groups that have locations setting set appropriately and this is what I shoot for unless there is some requirement that dictates otherwise.

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.