Random Row from ACF Repeater in Bricks

In the past, we shared how the first row of a ACF Repeater can be output in a Bricks query loop here.

This Pro tutorial shows how the query results can be filtered to return a random row.

Step 1

Enable query loop on an element and select your ACF Repeater as its query type.

Step 2

Add the following in child theme‘s functions.php or a code snippets plugin:

<?php

add_filter( 'bricks/query/result', function( $result, $query_obj ): array {
	// ensure that ACF is active and the query loop element is one with ID of hysbzs
	if ( ! class_exists( 'acf' ) || $query_obj->element_id !== 'hysbzs' ) {
		return $result;
	}

	return array_slice( $result, rand( 0, count( $result ) - 1 ), 1 );
}, 10, 2 );

Replace hysbzs with the Bricks ID of your query loop-enabled element.