Customizing ACF Repeater/Relationship or Meta Box Relationship Query Parameters in Bricks

For query types other than post, term and user in Bricks i.e, ACF Repeater/ACF Relationship/Meta Box Relationship there are no built-in controls for modifying the parameters like being able to limit the result count or show the results in random order.

But Bricks does provide a filter to do this programmatically called bricks/query/run.

Here’s an usage example.

Let’s say you have a Meta Box Relationship between two Custom Post Types called “Service to Review”.

Now, in the single service template you want to show the review CPT posts that are related via this relationship.

To limit the posts count to say 4 and to show them in random order, add the following in child theme‘s functions.php or a code snippets plugin:

add_filter( 'bricks/query/run', function( $results, $query_obj ) {
	if ( $query_obj->element_id !== 'nhuhtp' ) return $results;

	// limit the array count to 4
	$results = array_slice( $results, 0, 4 );

	// set random order
	shuffle( $results );

	return $results;
}, 30, 2);

Replace nhuhtp with the Bricks ID of your query loop element.

Instant access to all 250+ Bricks code tutorials with BricksLabs Pro


Leave your comment