Limit the Number of Posts in a Bricks Query Loop of Relationship Type

Updated on 12 Dec 2023

Bricks Query Loop popup does not have a control for setting the number of posts to be output when the query type is a non-standard one like ACF Repeater, ACF Relationship, Meta Box Relationship.

This Pro tutorial shows how.

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

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

  // return the first 2 results
  return array_slice( $result, 0, 2 );
}, 30, 2 );

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

Replace 2 with how many ever posts you want to be output.