Support

Account

Home Forums General Issues Update ACF field programatically that has not been 'initialised'

Solved

Update ACF field programatically that has not been 'initialised'

  • I am using learndash and want to record who approves as assignment. So I have created an ACF text field (assignment_approved_by) that is shown when the post type is assignment. The workflow is that the user uploads an assignment, and the teacher is notified. The teacher can login (front or backend) and ‘Approve’ the assignment. When the status of the assignment is changed to approve, I want to capture the user name in the ACF field. MY code below successfully fires when the assignment status is changed to ‘Approved’. It returns the user_id and from that gets the user display name of the user that has approved the assignment. I then want to update the ACF field with the user display name. I have tried both:

    $update_field($field_key, $display_name, $assignment_id);

    and

    $update_field('assignment_approved_by', $display_name, $assignment_id);

    Both of these lines crash the site. Here is my full code.

    
    //Record who approved an assignment
    function ofc_assignment_approval($assignment_id) {
        
        $user_id = get_current_user_id();
        if (!$user = get_userdata($user_id)) {
            return;
        }
        $display_name = $user->data->display_name;
        $field = acf_get_field( 'assignment_approved_by' );
        $field_key = $field['key'];
    
        $update_field($field_key, $display_name, $assignment_id);
    }
    add_action( 'learndash_assignment_approved',  'ofc_assignment_approval', 10, 4 );
    

    I have checked that $display_name, $field_key, and $assignment_id all have correct values.

    Hopefully someone more experienced than me can help.

  • You have to know the field key, you cannot get the field key by using the field name if that field does not have a value for the post, even if you use acf_get_field(). In this case your line $field = acf_get_field( ‘assignment_approved_by’ );is returningNULL`.

    In addition to this an ACF field stores the User ID in the database for a user field and not the users display name. The user ID is the value you need to use in the update.

  • Hi John,

    Thanks for your reply. When I output these variables to a file, I get the following, which suggests $field_key is returning something, but I assume that field key is incorrect?

    $user_id is set to the correct user id
    $display_name is set to the correct display name
    $field_key is set to ‘field_62146f78b78ee’

    Assuming the above field key is incorrect, how can I find the field key for the ACF field – ‘assignment_approved_by’? Is it listed in the admin section somewhere where the ‘assignment_approved_by’ field was created?

  • Update: I found how to find the field key and the field key for ‘assignment_approved_by’ is the same as the one I get in my code from using the code above.

  • You are doing this

    
    $update_field($field_key, $display_name, $assignment_id);
    

    you need to do this

    
    $update_field($field_key, $user_id, $assignment_id);
    

    and if your field allows multiple selections then you need to do this

    
    $update_field($field_key, array($user_id), $assignment_id);
    

    actually, you may need to do the second one even with only allowing a single user to be selected. I don’t remember if ACF stores an array or not for user fields that only allow one selection.

  • Hi John,

    Turns out I had it right all along, but had a blindingly obvious error! I had the $ sign in front of update_field!

    $update_field($field_key, $display_name, $assignment_id);

    So obvious that I missed it completely. Thanks for your replies though – it is much appreciated.

  • I didn’t see that either

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

You must be logged in to reply to this topic.