Support

Account

Home Forums General Issues Count repeater rows, add rows, recount = same result value

Solved

Count repeater rows, add rows, recount = same result value

  • Hi guys, I bought your plugin recently and I love it.

    However I’m running into a small problem and I’m not sure if I’m doing something wrong or I’ve encountered a bug.

    In simple examples, I’m trying to count the rows in a repeater array, add some rows, then count the rows again.

    So first of all I save the count then echo it:

    $Choicerows = get_field($choice_field_key,'user_'.$user_id);
                                $Choicerow_count = count($Choicerows);
                                $Mediarows = get_field($event_field_nameMedia,'user_1');
                                $Mediarow_count = count($Mediarows);
                                echo "$Choicerow_count | $Mediarow_count</br>";

    Then I add a few rows:

                                $newrow = array('choose_a_winner'	=> Right, 'stake'	=> "0");
                                add_row( $choice_field_key,$newrow,'user_'.$user_id);
                                $newrow = array('choose_a_winner'	=> Right, 'stake'	=> "0");
                                add_row( $choice_field_key,$newrow,'user_'.$user_id);
                                $newrow = array('choose_a_winner'	=> Right, 'stake'	=> "0");
                                add_row( $choice_field_key,$newrow,'user_'.$user_id);

    Then I re-count the rows and echo it:

                                $Choicerows = get_field($choice_field_key,'user_'.$user_id);
                                $Choicerow_count = count($Choicerows);
                                $Mediarows = get_field($event_field_nameMedia,'user_1');
                                $Mediarow_count = count($Mediarows);
                                echo "$Choicerow_count | $Mediarow_count</br>";

    Both counts return the same values despite the fact that I can see the new rows added to the user profiles.

  • *Edit*

    If using a completely empty repeater field then the count is 1 and the code will read the changes. However, any further added rows are not counted until the page is refreshed.

  • This is just a guess, but it sounds like a problem with the wp cache. Try cleaning the cache for the post after you add the new rows and before you try to count them again. https://codex.wordpress.org/Function_Reference/clean_post_cache

    
    <?php clean_post_cache( $id ) ?>
    
  • I’ll post the reply from support in case this helps anyone else. I don’t quite understand the acf/save_post feature yet but I’ll try and work it out.

    To get the new values of the number of rows that have been added, I would recommend you call the get_field() function from the acf/save_post and then get the new count so that it can reflect the new values after the update_field() has been called.

    You will need to call the action with a priority of greater than 10 to get the new values.

    Please have a look at the following resource page for more info on this: https://www.advancedcustomfields.com/resources/acfsave_post/

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

The topic ‘Count repeater rows, add rows, recount = same result value’ is closed to new replies.