Load data in random order from ACF Repeater field

December 20th, 2017 PHP 840 views

WordPress ACF(Advanced Custom Fields) plugin provides a really nice feature of repeatable fields. So you can add set of sub fields easily and then fetch them on frontend using a very simple loop. You can make any kind of field repetitive. So it can be textbox, selectbox, date picker, image upload and many more. You can even create group of different types of fields and make a whole group repeatable.

Another great feature here is that you can drag and drop the data to change their display order. But recently I came to a situation for which they don’t provide some helper function or any such thing. I don’t blame them, what they have provided is more then enough. So the requirement I had was that how to fetch the data added using repeated fields on frontend in Random order.

I found some tutorials on inter-web. Which were good but had some errors. Then realized that the repeatable fields can be stored in an array using PHP. And using PHP shuffle() function we can shuffle the array elements. So they organize in random order.

Below is a sample code of how I implemented.

Hope you find it helpful. Don’t forget to leave your comment and share with others.

Thank You.

Leave your comments: