Support

Account

Home Forums Backend Issues (wp-admin) update_field for User data type

Helping

update_field for User data type

  • English is not my native language, but i’ll try to make it verbose.

    I have a problem, how do i update_field() for a User data type?

    The way i do it is this:

    update_field(‘user’, 27, $new_post_id ); // 27 is the example ID.

    Now, technically it works in the back end, i can also fetch the value on my program, which will be 27. I can also fetch the objects by the user_id in WordPress by using

    $args = array(
    ‘posts_per_page’ => 99,
    ‘category_type_status’ => $inprogress_or_complete,
    ‘orderby’ => ‘date’,
    ‘order’ => ‘DESC’,
    ‘post_type’ => ‘booking_ojek’,
    ‘post_status’ => ‘publish’,
    ‘meta_query’ => array(
    array(
    ‘key’ => ‘user’,
    ‘value’ => $user->ID,
    ‘compare’ => ‘=’
    )
    )

    );

    No problem…………BUT here’s the problem that makes me stuck.

    IF i update the value in the backend, and changes it into, let’s say, “Agus” which is user 26. And then i saves it. Suddenly the value is not simply a number, it’s a serialized array. So it messed up my logic and program.

    My question is simple. Can you please, please…. give me an example of how to update_field for a user data type? so i won’t make a mistake. And how do i actually fetch the objects in WordPress using the id? because i can’t simply use the “=” comparison now, because it’s a serialized array. I assume i need to use “like” comparison? but that would not work because ID is just a number, so ID of 1 can also fetch posts that were associated to user with ID 21, because 21 have the number 1 in it.

    Thank you! and please help, i am REALLY stuck.

  • Hi @raymondseger ,

    Thanks a lot for reaching out and apologies for the delayed response.

    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’, …);

    I hope this helps.

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

The topic ‘update_field for User data type’ is closed to new replies.