Support

Account

Home Forums General Issues Populate a repeater select field depending on other subfield value (no ajax) Reply To: Populate a repeater select field depending on other subfield value (no ajax)

  • Your developer needs to use an acf/prepare_field filter using the field key varient. This filter is called before each instance of the field in the existing repeater.

    $field['name'] will have values that look like this:

    
    acf[field_61f2e6625ac01][row-0][field_61f2e66c5ac02]
    acf[field_61f2e6625ac01][row-1][field_61f2e66c5ac02]
    acf[field_61f2e6625ac01][row-2][field_61f2e66c5ac02]
    etc...
    

    This value needs to be parsed to determine what row of the repeater is being populated. There is also one that looks like this that needs to be ignored

    
    acf[field_61f2e6625ac01][acfcloneindex][field_61f2e66c5ac02]
    

    something like

    
    // this code has not been tested
    preg_match('/\]\[row-([0-9]+)/', $field['name'], $matches);
    if (!empty($matches[1])) {
      $row_index = intval($matches[1];
    }
    

    Using this information the value of the corresponding country field must be retrieved. I am not sure of the best way to do this. There is a chance that trying to read the repeater that is currently in the process of being displayed could cause the display loop to do odd things. I have never tried this. It may be necessary to use the field names in this instance an use get_post_meta() rather than using ACF functions. This can be done by constructing the correct meta_key
    "{$repeater_name}_{$row_index}_{$sub_field_name}"

    Then the choices of the current field can be populated based on the value of “country”