Home Forums Bug Reports ACF update_field() reorders my data


ACF update_field() reorders my data

  • Hi, I’m trying to use update_field() to update an ACF relationship field with an array of IDs. Immediately before running the function it outputs as array(7299, 3368, 3514, 3515, 7300) but afterwards it’s saved into the database as array(3368, 3514, 3515, 7299, 7300) (or rather the serialised equivalent). How can I stop it from rearranging the order?

  • What code are you using to generate the array before you update it?

  • 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!

  • I’m not sure if this is completely solved. I’m not doing anything within hooks to alter data, however, in any version of ACF passed 5.7.10, or 5.8.beta-3 will completely reorder fields when saving as acf-json; It’s very apparent when combined with version control.

    My team has confirmed rolling back to ACF 5.7.10 or 5.8.beta-3 is the only solution to prevent future field updates to acf-json from reordering.

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

The topic ‘ACF update_field() reorders my data’ is closed to new replies.