Home Forums General Issues How to update (add to) a user field via php


How to update (add to) a user field via php

  • So I have a field (user field where you can add multiple users) but how can I add to this via a php call please? Usually I would do update_field but in this case where it can hold X user id’s etc it is not going to work.

  • The user field holds an array of user IDs.

    If you want to add a user ID to the field that already has values.

    // get the user field without formatting
    $users = get_field('user_field', $post_id, false);
    // test to see if empty
    if (empty($users)) {
      $users = array();
    // add a user
    $users[] = $new_user_id;
    // use field key to update field
    // it will not update using field name
    // if it did not already have a value
    update_field('field_XXXXXXX', $users, $post_id);
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.