Support

Account

Home Forums General Issues Change user type field's return from array to ID, every CPT needs to be updated?

Solved

Change user type field's return from array to ID, every CPT needs to be updated?

  • I have a ACF field that takes users as value (also same problem with field that takes relationship as value). I had set the return value type to array at first, but now I would like to change it to ID. If I change the return value type to ID, all the existing posts that have that value submitted give an error on my template. The value is still in array form for those until I open the post editing and save it once without changing any values etc. Saving/updating the post updates the value from array to id.

    Is there any way to update ACF values for a mass of CPT posts? I have around 3000 posts with a wrong type value and updating them seems impossible…

  • There is no need to update any type of field if you change the return value. ACF always stores the same value in the database no matter what the return value is. The value you get is not determined until after ACF loads that value when it formats it.

  • But I am having this problem. I have posts that have ACF field where is user-type data stored. Before it was array, now I changed it to ID. When I am trying ti get_field, I don’t get id, I get an array. If I go to edit the post and save it without any changes, the value is presented as ID (and no longer as the faulty array).

  • A user field always holds an array of user IDs. This might change if you only allow one user to be selected, but I don’t think so. You must still loop over the even when you are returning IDs.

    
    $users = get_field('user_field_name');
    // just in case it has a single value
    if (!is_array('$users') && !emapty($users)) {
      $users = array($users);
    }
    if ($users) {
      foreach ($users as $user_id) {
        // do something with user id
      }
    }
    
  • I resolved the problem, it seems that the problem was not the field itself – it was the acf-getallobjects add-on I was trying to implement. Just wanted to post it here so that people with similar problems might find a solution 🙂

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

The topic ‘Change user type field's return from array to ID, every CPT needs to be updated?’ is closed to new replies.