Support

Account

Home Forums General Issues Strange behaviour with an ACF list

Solved

Strange behaviour with an ACF list

  • Hi, I have a strange behaviour with an ACF list and made this example to ask my question.
    With only $zero = true, my_field in the my_list is set to 0, everything is ok.
    With also $plus_one = true, the 3rd position is not 0 + 1, but the value, it had before, for an example, 7 + 1.
    With also $add_one = true, a new position is added, but _all_ changes before are gone (or seem to never been done).
    It seems to me, that there is a cache somewhere!?
    How can I change this behaviour? I want, of course, all changes to work correctly and sequentially in my order.
    Am I perhaps doing something the wrong way? Is there something aynchroneous?
    Could some plugin be the reason?

    $zero = true;
    $plus_one = true;
    $add_one = true;
    
    $example = get_post(7102);
    
    if($zero)
    {
        if(have_rows('my_list', $example->ID))
        {
            while(have_rows('my_list', $example->ID)): the_row();
    
            update_sub_field('my_field', 0);
    
            endwhile;
        }
    }
    
    if($plus_one)
    {
        $pos = 0;
        if(have_rows('my_list', $example->ID))
        {
            while(have_rows('my_list', $example->ID)): the_row();
    
            $my_field = get_sub_field('my_field');
    
            if($pos == 2)
            {
                $my_field += 1;
                update_sub_field('my_field', $my_field);
            }
    
            $pos++;
    
            endwhile;
        }
    }
    
    if($add_one)
    {
        $new = [];
    
        $new['my_field'] = 0;
    
        add_row('my_list', $new, $example->ID);
    }
  • Hello John,

    thanks for your answer!

    I tried using the field-keys (example: field_5fd76d9f2cf06) of both the list-field and the sub-field,
    I also tried list-field-key and sub-field-name,
    I also tried list-field-name and sub-field-key.

    Unfortunately, I got the same behaviour in all cases.

  • Does this happen when adding a row to a repeater that does not already have rows?

    Going back and reading the OP I am a little confused about what the issue really is.

  • Hello John,

    the problem only occurs with existing rows…
    Explained in other words:

    When I use get_sub_field after I used update_sub_field,
    I get the old value from before the script,
    not the new just updated value.

    When I use add_row,
    everything done before with update_sub_field is gone.
    It seems to me that a cache is used.

    This only happens if the steps are in the same php script execution.
    If I do it later / on another page, it works fine.

  • Okay, I think your having a caching issue. It appears that updating the sub field is not clearing either the ACF cache for the field or the WP meta cache for the field.

    Going to be honest, I don’t know if there is a way to update a sub field and then immediacy get the value that was just saved during the same request (page load). Generally updating of fields and reloading of fields happens in separate requests. I’ve just taken a look at the ACF code and I don’t see any apparent way to clear the current value old value from the cache.

    You might want to ask the developer about this one to see if he has any advice https://www.advancedcustomfields.com/contact/

  • Hello John,

    okay, thank you for your time! ๐Ÿ™‚

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

You must be logged in to reply to this topic.