Support

Account

Home Forums ACF PRO Populate choices to conditional logic

Solving

Populate choices to conditional logic

  • Hi !
    I have a question about populating choices in the admin part of ACF.
    For now I know how to populate choices to selection fields with acf/load_field.
    I got an options page in which I want my users make some choices and I want these choices affect the way fields are displayed or not in another group.
    So here is my question : is there a way to populate these choices in order to activate conditional logic in another group ?

    Thanks a lot for any thoughts or insights.

  • Yes, you can populate conditional logic, and you can even make it work across field groups, using the acf/load_field filter https://www.advancedcustomfields.com/resources/acf-load_field/

    The best advice I can give you is to create a field group that has a select field with some example choices and some other fields that are conditional on those choices an then export that group to PHP to see what the settings are.

    This cannot be done across field groups in the ACF admin, but it can be done with the acf/load_field filter because the conditional login is based on the field key. Your conditional logic will work as long as that field key is on the page. However, you must make sure it’s on the page because otherwise you might trigger some JS errors if ACF tries to find a field that does not exist.

  • Despite there’s no conditional logic yet on image select, I don’t figure out how I can set the conditional logic with acf-load-field.
    Do you have any example ?

  • I am still looking for this !!

  • Let’s say you have two simple text fields, and only want Field B to be visible when Field A has a value entered (someone has typed in the field).

    Go to Custom Fields > Tools.

    Select the field that you wish to alter under “Export Field Groups” and then click “Generate PHP.”

    Find the keys of the fields that you wish to manipulate in the resulting PHP.

    The text fields look something like this (I omitted many parts of each array, and simplified what the key will actually look like to make it easier to follow):

    
    array(
      'key' => 'field_645328a8347d',
      'label' => 'Field A',
      'name' => 'field_a',
      'type' => 'text',
      ...
    ),
    array(
      'key' => 'field_645308d746c6',
      'label' => 'Field B',
      'name' => 'field_b',
      'type' => 'text',
      ...
    )
    

    Now you can write your filter function:

    
    function modify_field( $field ){
    
    $field['conditional_logic'] = array( 
      array(
        array(
          'field' => 'field_645328a8347d',
          'operator' => '!=empty',
          'value' => ''
        )
      )
    );
    
    }
    add_filter('acf/load_field/key=field_645308d746c6');
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Populate choices to conditional logic’ is closed to new replies.