Get Data in Random Order From an ACF Repeater Field

WordPressPosted on

Advanced Custom Field is one of my favorite WordPress developer plugins. The repeater field is a feature that is genuinely missing from the WordPress core.

Using the repeater field, we can create any combination (row) of a set of subfields again and again.

Using the field, we can retrieve the saved data through a simple loop. Although one thing is not that easy, to get the rows in a randomized order. To achieve this, we have to use the shuffle() PHP function:

if (have_rows('partner', 'option')) {
    $partners = get_field('partner', 'option');
    shuffle($partners); // Randomize

    foreach($partners as $partner) {
        echo $partner['name'];
        echo $partner['url'];

Here, instead of the classic while, we use a foreach to loop the array, and we don’t use the the_row() function, so we have to access the values through an index name instead of the get_sub_field() function.

Need a web developer? Maybe we can help, get in touch!

Similar Posts

More content in WordPress category