Skip to content

Instantly share code, notes, and snippets.

@wesrice
Created February 27, 2012 16:02
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save wesrice/1924934 to your computer and use it in GitHub Desktop.
Save wesrice/1924934 to your computer and use it in GitHub Desktop.
Return a random row of data from an ACF repeater field
<?php
// Get the repeater field
$repeater = get_field( 'repeater_field_name' );
// Get a random rows. Change the second parameter in array_rand() to how many rows you want.
$random_rows = array_rand( $repeater, 2 );
// Loop through the random rows if more than one is returned
if( is_array( $random_rows ) ){
foreach( $random_rows as $random_row ){
// Output data here. Replace sub field names.
echo 'Sub Field 1: ' . $repeater[$random_row]['sub_field_1'] . '<br/>';
echo 'Sub Field 2: ' . $repeater[$random_row]['sub_field_2'] . '<br/><br/>';
}
} else {
// Output data here. Replace sub field names.
echo 'Sub Field 1: ' . $repeater[$random_rows]['sub_field_1'] . '<br/>';
echo 'Sub Field 2: ' . $repeater[$random_rows]['sub_field_2'] . '<br/><br/>';
}
?>
@bridgetwes
Copy link

Thanks for the tip!

If you only want one random item from ACF repeater, you can also do:

$repeater = get_field( 'repeater_field_name' );
$rand = rand(0, (count($repeater) - 1));
echo $repeater[$rand]['sub_field_1'];

@IntrepidRealist
Copy link

Bridgetwes,
You saved my a$$! Thank you!

@psorensen
Copy link

Thanks!

@davemac
Copy link

davemac commented May 11, 2016

I had an issue with the rows not being random in PHP 5.6, by using shuffle() it solved the issue http://stackoverflow.com/questions/20163709/php-array-rand-not-working-properly-when-randomizing-more-then-1-random-arraykey

@csaborio001
Copy link

Sweet - thank you!

@doopsi
Copy link

doopsi commented Feb 13, 2019

Thanks for the tip!

If you only want one random item from ACF repeater, you can also do:

$repeater = get_field( 'repeater_field_name' );
$rand = rand(0, (count($repeater) - 1));
echo $repeater[$rand]['sub_field_1'];

Thanks!

@PaulBart
Copy link

PaulBart commented May 8, 2019

Great thanks! I'm trying to get a version that will also "reload" the 3 random rows I had displayed every few seconds?

@Garconis
Copy link

8 years later, this still works great!

@RemiBLoizeau
Copy link

RemiBLoizeau commented Mar 17, 2021

9 years later too. Thanks.

@rovente
Copy link

rovente commented Mar 17, 2022

To keep ACF syntax, like get_sub_field(), nested repeaters, etc. i suggest to go with this:

<?php

// Randomize and shuffle the rows
$rows = get_field('repeater_field');
shuffle($rows);
$rand_repeater_fields = array_rand( $rows , 4 );
?>

<section>
	<?php if( have_rows('repeater_field') ):
		$stage_index = 0; ?>
	  <?php while ( have_rows('repeater_field') ) : the_row();

			// print rows only if in array
			if (in_array(get_row_index() - 1, $rand_repeater_fields)) { ?>

				<?php // do your stuff ?>
				<?= get_sub_field('repeater_subfeld') ?>

				<?php if( have_rows('another_repeater_field') ): ?>
				  <?php while ( have_rows('another_repeater_field') ) : the_row(); ?>

						<?php // do your stuff ?>
						<?= get_sub_field('another_repeater_subfeld') ?>
						
				  <?php endwhile; ?>
				<?php endif; ?>

			<?php // increment index
				$stage_index++;
			} ?>

	  <?php endwhile; ?>
	<?php endif; ?>
</section>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment