Home Forums Bug Reports ACF update_field() reorders my data Reply To: ACF update_field() reorders my data

  • Ahah, found it.

    It was because the array wasn’t using a standard [0] [1] [2] key sequence. It saved it in the database as expected, but when it output it on the front-end / admin area that it rearranged it by key into ascending numerical order.

    Luckily I didn’t need to keep the keys, so I reset them by using array_values() before putting through the update_field() function.


    update_field(‘content_blocks’, $array, $post_id);`

    … gives us…

    array(5) { [5956]=> int(7299) [5107]=> int(3368) [5954]=> int(3514) [5955]=> int(3515) [5957]=> int(7300) }

    …and it saves the following into the database:


    …and that outputs on the front end in the wrong order.

    My fix was to reset the array keys to a basic version like:

    $cleaned_array = array_values($array);
    update_field(‘content_blocks’, $cleaned_array, $post_id);`

    …which gives the same output, but saves the following into the database:


    …which does output in the correct order.

    That’s enough to fix the issue in my case. Thanks for your time!