Support

Account

Home Forums ACF PRO Adding a repeater row to a user

Solved

Adding a repeater row to a user

  • I have a repeater assigned to all users which has a sub-field with users.
    (screenshot)

    I want to add a field to a user via PHP.
    I’m currently using:

    $add_thisone = add_row('matched_users', $userArrayData, 'user_' . $userID);

    But I’m getting a blank repeater field when viewed in the backend.
    (screenshot)

    Is there a proper way to retrieve user data from a user ID, name, e-mail or by other means?

    I’m only getting blank repeater fields even when entering identical var_dump() values of the user field.

  • Hi @acfarx-pw

    The add_row() function needs the repeater field to be initialized in the database first. It means you need to save it at least once even with an empty value.

    Could you please try the update_field() function instead? This page should give you more idea about it: https://www.advancedcustomfields.com/resources/update_field/.

    I hope this helps.

  • Hi James, I’ve taken your advice and I’m able to add to one repeater field on one user, but not another when the database isn’t initialized for that user.
    ( my code is below )

    
    $field_key = 'matched_users';
    //$user1 is user1's ID
    //$user2 is user2's ID
    echo 'adding field...<br>';
    
    $post_id1 = 'user_' . $user2;
    $value1 = get_field($field_key, $post_id1);
    $value1[] = array("user" => $user1);
    update_field($field_key,$value1,$post_id1);
    
    $post_id2 = 'user_' . $user1;
    $value2 = get_field($field_key, $post_id2);
    $value2[] = array("user" => $user2);
    update_field($field_key,$value2,$post_id2);
    
    echo '<br>added';
    

    How would i initialize the database? With a blank add_row/update_field?
    Thank you for your help so far.

  • Hi @acfarx-pw

    Please use field key instead of field name for the $field_key variable. A field key looks something like this: field_1234567890abc. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/update_field/#field_key-vs%20field_name.

    I hope this helps.

  • Hi James, I’m now using the field key but I still cannot add new fields to new users without adding a field in the back-end first.

    How would I resolve this?
    EDIT: A bit of code I wrote wasn’t making this function, the above solution works.

    
    $field_key = 'field_570d00f097fbd';
    //$user1 is user1's ID
    //$user2 is user2's ID
    echo 'adding field...<br>';
    
    $post_id1 = 'user_' . $user2;
    $value1 = get_field($field_key, $post_id1);
    $value1[] = array("user" => $user1);
    update_field($field_key,$value1,$post_id1);
    
    $post_id2 = 'user_' . $user1;
    $value2 = get_field($field_key, $post_id2);
    $value2[] = array("user" => $user2);
    update_field($field_key,$value2,$post_id2);
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Adding a repeater row to a user’ is closed to new replies.