Support

Account

Home Forums Add-ons Repeater Field Dynamically Populate Repeater Field with Users

Solving

Dynamically Populate Repeater Field with Users

  • I have a repeater field with two sub fields (user and commission). I need to dynamically create as many rows as users (specifically users marked as editors) and dynamically populate each user (who is an editor) in the user subfield.

    Any help is appreciated!

  • There isn’t enough information to give an exact answer, but in general you can pre-populate a repeater this way.

    
    // this filter has the field key of the repeater field
    add_filter('acf/load_value/key=field_123456', 'my_repeater_values', 20, 3);
    function my_repeater_values($value, $post_id, $field) {
      
      // generate rows of the repeater
      // see next code snippet
    
      return $value;
    }
    

    The value you generate needs to be a nested array

    
    // $value format
    $value = array(
      // you add a nested array for each row
      array(
        // this nested array holds the field key => value pairs for each sub field
        'field_234567' => 'value of sub field 1',
        'field_345678' => 'value of sub field 2'
      ),
      // second row of repeater
      array(
        // this nested array holds the field key => value pairs for each sub field
        'field_234567' => 'value of sub field 1',
        'field_345678' => 'value of sub field 2'
      ),
      // etc....
    )
    

    this does not include any checking. For example, you probably do not want to overwrite existing values/rows and want to add new rows when new users are added. I also don’t know exactly what information you want to store.

    However, I would probably store the commission information you want to store as a custom field on the user profile page in a field group that is only available to site admins and then get the information from there when I need it.

  • Thank you for your response! The main group is a transaction for real estate. I have dozens of fields about the transaction and one of the fields is a commission repeater to add each agent and their commission for this particular transaction. I plan to calculate it by default (which is a different topic), but right now I’m focused on a number field. I would need to update the calculation in many cases, anyway, as commissions have a default, but vary for all sorts of reasons on individual transactions.

    So, I have a repeater and I need a row for each user who is the type, “editor”. I would not want to add the rows, if there are already rows in the repeater. (As you said, I wouldn’t want to add users to a transaction a year from now when the users have changed and the transaction already has users with commissions assigned.)

    From your post, I don’t understand how the value works. It seems as though I have to define each possible row explicitly, rather than grab the users, dynamically. Can you help me understand?

  • It is possible to create each row individually, but for your purposes, it sounds like you want to create them once for this repeater and never update them again.

    I find it much easier to create the entire repeater in one go and on top of this, you can’t have different values automatically generated for each row of the repeater. For example, you can’t use an acf/load_value filter on one of the sub fields, the value generated for this sub field will be the same for every row you generate. The only real way to dynamically generate these values and have a different value for each row is to do them all at once.

    Also, when dealing with fields that do not already exist it is always better to use field keys. 99% of the time if you use the field name for fields that have not already been saved then you won’t get the results your looking for.

    So, what you need to do is add an acf/load_field filter for the repeater field.

    
    add_filter('acf/load_value/key=field_123456', 'editor_commission_repeater_values', 20, 3);
    
    function my_repeater_values($value, $post_id, $field) {
      
      // If the value of this field has never been set then it will === false;
      if ($value !== false) {
        // the value is not false, so it has already been saved
        // do not regenerate this field
        return $value;
      }
      
      // get all editors
      // only get the user ID because this is what 
      // ACF needs to store for a user type field
      $args = array(
        'role' => 'editor',
        'fields' => 'ID'
      );
      $users = get_users($args);
      // make sure we got some users
      if (!$users) {
        // if not then no need to continue
        return $value;
      }
      
      // generate rows of the repeater
      
      // initialize array
      $value = array();
      
      foreach ($users as $user) {
        // add a row to the value for this user
        $value[] = array(
          // the first field is the user field
          // use the field key
          'field_567890' => $user,
          // the second field is a number field, but could be anything
          'field_876543' => 100 // assuming number field and default value
        );
      }
      
        
      // return the generated repeater
      return $value;
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Dynamically Populate Repeater Field with Users’ is closed to new replies.