Forum Replies Created

  • I just have a quick follow-up question to this original question, and didn’t want to clutter the forum with additional posts. If I should make an additional post, please let me know.

    So everything is working perfectly fine up until this point. I am able to add and remove Users as I please, so long as the User Field is initially populated in the Post.

    However, when using a Multiple Select User Field, if no User is initially populated in the Post, and the following command is run:

    $test = get_field('cb_roster', false, false);

    This returns nothing at all. Therefore, if I use something like:
    array_push($test, $current_user);

    Nothing will happen (since it doesn’t exist).

    So, if I run the following:

    $current_user = get_current_user_id();
    $current_user = strval($current_user); //converting to string just in case
    $test = get_field('cb_roster', false, false);
    $current_user = array($current_user);
    update_field('cb_roster', $current_user);

    And then run:

    $test = get_field('cb_roster', false, false);

    I get a blank value (as though it does not exist).

    The output for print_r($current_user) returns:
    Array ( [0] => 24 )

    Which matches the format of the Array if it were populated from the Post.

    Any input is greatly appreciated!

  • I got it… Turns out I was thinking too hard…

    I re-read the documentation for get_fields().

    Answer was that I don’t need to provide the full array as I was doing. Instead, the answer was to return ‘cb_roster’ like so:

    $roster = get_fields('cb_roster', false, false);

    Which returns the array without formatting. The array without formatting looks like this:


    Therefore, I only need to pass this script:

    $value = '21';
      array_push($roster, $value);
      update_field('cb_roster', $roster);

    Sorry for the confusion…

  • Okay, here’s the ultimate test in my opinion, and I have it narrowed down to my approach being completely wrong. The only problem is, I don’t know why it’s wrong?

    If I run:

    $roster = get_field('cb_roster');
    update_field('cb_roster', $roster);

    And then after that script runs, I check the field:

    $roster = get_field('cb_roster');

    I get the output:
    Array ()

    What am I missing? To me, this is like being handed a box with a ball inside. I open the box, put the ball on the table. I put the ball back in the box, and now the box is empty…

    Any help is greatly appreciated.

  • My apologies, I knew in my wall of text I would omit things. That code in its entirety is actually –

      $value = '21';
      // format value
      if( !$value || $value == 'null' )
        return false;
      // temp convert to array
      $is_array = true;
      if( !is_array($value) )
        $is_array = false;
        $value = array( $value );
      foreach( $value as $k => $v )
        $user_data = get_userdata( $v );
        //cope with deleted users by @adampope
        if( !is_object($user_data) )
          unset( $value[$k] );
        $value[ $k ] = array();
        $value[ $k ]['ID'] = $v;
        $value[ $k ]['user_firstname'] = $user_data->user_firstname;
        $value[ $k ]['user_lastname'] = $user_data->user_lastname;
        $value[ $k ]['nickname'] = $user_data->nickname;
        $value[ $k ]['user_nicename'] = $user_data->user_nicename;
        $value[ $k ]['display_name'] = $user_data->display_name;
        $value[ $k ]['user_email'] = $user_data->user_email;
        $value[ $k ]['user_url'] = $user_data->user_url;
        $value[ $k ]['user_registered'] = $user_data->user_registered;
        $value[ $k ]['user_description'] = $user_data->user_description;
        $value[ $k ]['user_avatar'] = get_avatar( $v );
      // de-convert from array
      if( !$is_array && isset($value[0]) )
        $value = $value[0];

    Sorry about that.

    So this was my code, but my issue from before still stands. I wouldn’t be able to output the array of $value and check the types using gettype as I previously mentioned if this was not the case.

    I updated my original post to reflect this, so it’s not confusing to someone reading it for the first time. Thanks!

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