Support

Account

Home Forums General Issues Problem using update_field with user field

Solving

Problem using update_field with user field

  • Hi, i´ve a custom field called “administradores”. Type of field is “User” and it supports multiple selection.

    If i update a post with that field on the WP Admin, i get something like that on wp_postmeta table:

    a:1:{i:0;s:1:”1″;}

    When i update that field using add_post_meta function, i use this:

    add_post_meta($post_id,’administradores’,$administradores);

    $administradores is an array of user IDs.

    However, if i update that field via WP API, using add_post_meta or ACF update_field function, i get something like:

    a:1:{i:0;i:1;}

    It looks like ACF works both ways, because y see the right data on WP admin. But the problem is that i have to use a reverse query on my theme, something like:

    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,
    array(
    ‘key’ => ‘administradores’,
    ‘value’ => ‘:’ . $current_user->ID . ‘;’,
    ‘compare’ => ‘LIKE’
    )
    )

    and it dont work if i update the field via webservice. I´m using ACFPro v5.2.3, but same thing happens with last version of ACF.

    How can i update a user field properly????

  • Hi @esedeerre

    For complex fields, ACF stores the value in wp_postmeta as a serialized PHP array.

    Hmm… I believe you should be able to update the user field using update_field(…) function. The code should look something like this:

    $administradores[] = $new_user_id;
    update_field( $field_key, $administradores, $post_id );

    where $new_user_id is the ID of a new user and $administradores contains user IDs of the previously selected users.

  • Thank you James.

    Using update_field i have the same result as using add_post_meta:

    a:1:{i:0;i:1;}

    Again, no quotes.

    I´ve tried also to pass an array of user objects, or force quotes on my array of ids, but it don´t work 🙁

  • Hi @esedeerre

    Hmm… On wp-admin, ACF saves the user ID as a string and that is why you get this: a:1:{i:0;s:1:”1″;} therefore you should pass a string array of user IDs in update_field(…) to obtain the same results as in wp_admin i.e. something like this: array('1', '2', '3', ...);

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.