Home Forums Backend Issues (wp-admin) sort by adding a field with php (acf_add_local_field9 Reply To: sort by adding a field with php (acf_add_local_field9

  • The reason is that there is no solution, at least not an easy one.

    ACF adds fields to the group in the order that they are created. In order to insert a field between two other fields you would have to make changes and you would not be able to use acf_add_local_field()

    Some thoughts on how you can do this.
    One way is that you can add a filter between adding fields of your field group if you are doing this in PHP in the parent theme.

    // parent theme code
    // a field you are adding
    // where you want to allow insertion of other fields
    // the you would build and action filter that uses your hook
    // another field you are adding

    The problem with this is that you’d end up with dozens of hooks if you want to be able to insert fields between every other field.

    Another way would be to use the acf/validate_field_group filter hook. This is an undocumented hook in ACF. It passes the field group array before it is added. In order to use this your field group would need to be loaded from the DB, JSON or it the field group would need to be added using the PHP generated by ACF when you export a field group to PHP.

    add_filter('acf/validate_field_group', 'add_fields_to_group');
    function add_fields_to_group($gorup) {
      // you need to loop over all the fields until you find 
      // the one before where you want to insert a new field
      $fields = $group['fields'];
      $new_fields = array();
      foreach ($fields as $field) {
        if ($field['name'] == 'field you want to insert after') {
          $new_fields[] = $field;
          $new_fields[] = array(/* array of field argument */);
        } else {
          $new_fields[] = $field;
      // set group fields to new fields and return
      $group['fields'] = $fields;
      return $group;