Home › Forums › Bug Reports › 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.
So…
`var_dump($array);
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:
a:5:{i:5956;s:4:"7299";i:5107;s:4:"3368";i:5954;s:4:"3514";i:5955;s:4:"3515";i:5957;s:4:"7300";}
…and that outputs on the front end in the wrong order.
My fix was to reset the array keys to a basic version like:
`var_dump($array);
$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:
a:5:{i:0;s:4:"7299";i:1;s:4:"3368";i:2;s:4:"3514";i:3;s:4:"3515";i:4;s:4:"7300";}
…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.
The topic ‘ACF update_field() reorders my data’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.