Support

Account

Home Forums General Issues 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.

  • This could be easily solved is only there’s a way to store and return the User only with it’s ID instead of serialized array.

    You know, just like “Post Object” data type in ACF, and we choose return Format of “Post ID”.

    Anyone knows how to do this? thanks

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

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