Support

Account

Home Forums General Issues Update sub field, then get value

Solved

Update sub field, then get value

  • Welcome, I have the latest ACF pro and the latest WP. So I have this snippet code:

    update_sub_field(array("questions", 1, "votes"), 777, 368);
    var_dump(get_field("questions", 368)[0]["votes"])
    update_sub_field(array("questions", 1, "votes"), 888, 368);
    var_dump(get_field("questions", 368)[0]["votes"])

    – Post id: 368.
    – Repeater name: questions
    – Row: first
    – sub field name: votes

    The problem is I need to get as result “777” and “888”, but I always I get one value, if I refresh page I see value change but the two values are always the same! What’s the problem here please, I also try use the function inside while loop but always same result.

  • I’ve got the exact same issue here.

    I have a update_sub_field inside a foreach after which I do a get_field of the repeater right after the update, but it still has the old values even though the update_sub_field did indeed update everything correctly.

    Not sure why ACF get_field is still getting old values. Thanks for any help!

    E.g.

    $lists = get_field('lists', $post->ID); // repeater field
    
    foreach ($lists as $key => $list) {
        update_sub_field(array('lists', $key, 'field'), 0, $post->ID); // switch from '1' to '0'
    }
    
    $lists = get_field('lists', $post->ID);
    
    var_dump($lists);
    
    // outputs old values
  • Hi al, Thanks for your reply, I managed to find a trick to solve this problem by using the native WordPress get_post_meta function, the idea is as follow:

    
    $lists = get_field('lists', $post->ID);
    
    $len= 0; // To get the number of rows
    foreach ($lists as $key => $list) {
      update_sub_field(array('lists', $key, 'field'), 0, $post->ID);
      $len++;  
    }
    
    $result = array();
    
    for($i= 0; $i < $len; $i++) {
      $result[$i]= array(
        "field" => get_post_meta($post->ID, "lists_".$i."_field", true),
      );
    }
    
    var_dump($result);
    

    You have just to take a look about how ACF name the post meta fields here: http://www.advancedcustomfields.com/resources/querying-the-database-for-repeater-sub-field-values/

    Regards

  • Hey ech,

    I managed to find a workaround shortly after posting to your own question, here’s the link: http://support.advancedcustomfields.com/forums/topic/get_field-after-update_sub_field-gets-old-values/

    My workaround method involves updating the lists field after setting the $list during the foreach. This way you you’re not forced in using WP code to access updated ACF repeater data.

  • In case someone is still having this issue I fixed it by calling this function:

    acf_flush_value_cache($post_id, $field_name);

    Like the name says, it flushes the cache so ACF can get the new values 🙂

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

You must be logged in to reply to this topic.