Load data in random order from ACF Repeater field
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.
$customer_reviews = get_field('customer_reviews');
shuffle($customer_reviews); // magic happens here :)
if( have_rows('customer_reviews') ):
foreach( $customer_reviews as $customer_review ):
$review_title = $customer_review['review_title'];
$review_description = $customer_review['review_description'];
$review_star_ratings = $customer_review['review_star_ratings'];
echo '<strong>Stars: '.$review_star_ratings.'</strong>';
Hope you find it helpful. Don’t forget to leave your comment and share with others.