Support

Account

Home Forums General Issues Use conditional logic across another group?

Solving

Use conditional logic across another group?

  • Hi,

    Is it possible to use conditional logic to hide fields from another group? For example: If a checkbox in checked in group A, then a checkbox in group b is hidden.

  • Yes it is, but not withing the admin. You need to create an acf/prepare_field filter and alter the conditional logic value of the field you want to be conditional. This takes and array that includes the field key, comparison and value.

    To get started, create a field that uses conditional logic and export it to php and you can see what you need to do to set it up.

  • Hi !

    I’m trying to do the exact same thing and have setup the conditional field keys inside my code.

    Everything is working as expected inside the same field-group but the conditional field is not taken into account by the field inside the second field-group.

    Could you help please ?

  • field keys are required for conditional logic

  • I don’t get it, isn’t that already what I’m doing since we can see in the last screenshot the same value inside the field’s data-key (in red) and the other field’s data-conditions (in blue) ?

  • It’s hard to tell what you are using but this looks like a field name “homepage_acf_after_title_cond_text” a field key would look something like “field_123456”

  • Oh ok sorry, those actually are field keys, I’m setting them programmatically when defining the fields by code. They are unique in the context of the post-type.

  • Field keys must start with “field_”

    Field group keys must start with “group_”

  • Alright I’ll try that, I didn’t know there was a prerequisite like that, I shipped about 20 projects without those prefixes and never had any issue (never tried to do conditions across field-groups though).

    I’ll let you know, thank you !

  • There are places where other field keys will work. There was a time that they worked everywhere, very long time ago. To be honest I’m surprised that ACF will even allow you to add fields that do not have correct field keys.

  • New project with only the ACF plugin (PRO) installed (5.10.2).
    I put this inside functions.php (generated by ACF and then modified to use the field key inside the conditional_logic array) :

    if( function_exists('acf_add_local_field_group') ):
    
      acf_add_local_field_group(array(
        'key' => 'group_616de148bf2ce',
        'title' => 'page after title',
        'fields' => array(
          array(
            'key' => 'field_616de15b4155f',
            'label' => 'Field in after-title group',
            'name' => 'test',
            'type' => 'text',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array(
              'width' => '',
              'class' => '',
              'id' => '',
            ),
            'default_value' => '',
            'placeholder' => '',
            'prepend' => '',
            'append' => '',
            'maxlength' => '',
          ),
        ),
        'location' => array(
          array(
            array(
              'param' => 'post_type',
              'operator' => '==',
              'value' => 'page',
            ),
          ),
        ),
        'menu_order' => 0,
        'position' => 'acf_after_title',
        'style' => 'default',
        'label_placement' => 'top',
        'instruction_placement' => 'label',
        'hide_on_screen' => '',
        'active' => true,
        'description' => '',
      ));
    
      acf_add_local_field_group(array(
        'key' => 'group_616de1da31d45',
        'title' => 'page normal',
        'fields' => array(
          array(
            'key' => 'field_616de1eb45ead',
            'label' => 'Field in normal group',
            'name' => 'hiding',
            'type' => 'text',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => [
              [
                [
                  'field' => 'field_616de15b4155f',
                  'operator' => '!=',
                  'value' => 'test'
                ]
              ]
            ],
            'wrapper' => array(
              'width' => '',
              'class' => '',
              'id' => '',
            ),
            'default_value' => '',
            'placeholder' => '',
            'prepend' => '',
            'append' => '',
            'maxlength' => '',
          ),
        ),
        'location' => array(
          array(
            array(
              'param' => 'post_type',
              'operator' => '==',
              'value' => 'page',
            ),
          ),
        ),
        'menu_order' => 0,
        'position' => 'normal',
        'style' => 'default',
        'label_placement' => 'top',
        'instruction_placement' => 'label',
        'hide_on_screen' => '',
        'active' => true,
        'description' => '',
      ));
    
    endif;

    I can see both fields on the page, but when I type “test” into the first one (inside the after-title group), nothing happens (the second field inside the normal group should disappear).

    BTW : thank you for your time ! It is really appreciated

  • going to be honest with you. I haven’t a clue. This used to work, but it seems that it no longer does.

    You should contact the developers because it appears that conditional logic only works in the same group even when coding.

    https://www.advancedcustomfields.com/contact/

  • thank you for your time ! It is really appreciated

  • Sorry, I would not have suggested this originally if I knew it did no work. The last time I tried it was working but that was a long time ago. I don’t know why they would change this.

  • I just tried using acf/prepare_field to alter the conditional_logicof a field in one group, that’s cloned in another group, but as a replace field, not as group, which means the UI won’t display the Conditional Logic checkbox.

    And it works.

    The only thing to note is that I had to use key={key of the clone field}_{key of the field in a cloned group}, something like 'acf/prepare_field/key=field_62704081a8489_field_62703f61a8487'

Viewing 15 posts - 1 through 15 (of 15 total)

You must be logged in to reply to this topic.