Support

Account

Home Forums General Issues Update_Field not working

Solved

Update_Field not working

  • Hey guys,

    I’m having trouble updating a select field, and hoping someone can assist me. This is what I have so far:

    $assessments_repeater = get_field('assessments','user_' . bp_displayed_user_id() . ''); // Get Assessments Repeater field
    $ass_latest = end($assessments_repeater); // Get last row of repeater
    $current_level = get_field('current_level','user_' . bp_displayed_user_id() . ''); //Get current level
    $ass_latest_rating = $ass_latest['ass_rating']; // Get latest rating
    $ass_at = $ass_latest['assessed_at']; // Get latest level assessed at
    $displayed_user = 'user_' . bp_displayed_user_id() . ''; // Get User ID
    
    if(($ass_latest_rating) != 1) {
      update_field($current_level,$ass_at, $displayed_user);
    }

    The code is working for the most part:

    echo $current_level; //returns 1
    echo $ass_at; //returns 2

    However, if I refresh the page, the $current_level remains on 1. If anyone can see what I’m doing wrong, I’ll greatly appreciate it!!

  • Hi @guit4eva

    If the field didn’t exist beforehand in the DB (created in some other way than this) you need to use the field key rather than the field name as the first parameter for update_field().

    As a sidenote you do not need to concatenate the strings with empty ” at the end 🙂

    Example:

    
    <?php
    $displayed_user = 'user_' . bp_displayed_user_id(); // Get User ID
    $assessments_repeater = get_field( 'assessments', $displayed_user ); // Get Assessments Repeater field
    $ass_latest = end($assessments_repeater); // Get last row of repeater
    $current_level = get_field( 'current_level', $displayed_user ); //Get current level
    $ass_latest_rating = $ass_latest['ass_rating']; // Get latest rating
    $ass_at = $ass_latest['assessed_at']; // Get latest level assessed at
    
    if( $ass_latest_rating != 1 ) {
      update_field( 'fieldkey goes here', $ass_at, $displayed_user );
    }
    
  • That worked a treat, thanks so much! 😀

    And thanks for pointing out the empty strings issue – don’t ask me why I had that there in the first place…ha ha 🙂

    Thanks again, have a great day! 🙂

  • No problem! Glad to help out.

    You have yourself a great weekend.

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

The topic ‘Update_Field not working’ is closed to new replies.