When I create a Query Loop and choose a Custom Post I get an option to sort the results by a field.
But if I create a Query Loop on a Repeater field group I get no such option.
now with Bricks v1.8, we can use the new bricks/query/result filter.
Put this into a code snippet or functions.php:
<?php
add_filter( 'bricks/query/result', function( $result, $query_obj ){
// Return: Element ID is not the right one
if ( $query_obj->element_id !== 'hmhovo' ) {
return $result;
}
if (!empty($result)) {
$title = array();
$z = 0;
foreach( $result as $item ) {
$z = $z + 1;
// for this example I retrieve the title of the post
$title[$z] = get_the_title( $item->ID );
}
array_multisort($title, $result);
}
return $result;
}, 10, 2 );
Replace the element ID hmhovo in the code with your element ID of the repeater or relationship query.
I’ve tried numerous combinations but it never seems to work. Not sure whether to add it to the actual queried block - as the field is within the query (it’s on the ACF).
Looks like nothing other than a custom function will work for this, at least for me I worked this out after hours of playing around:
add_filter('acf/format_value/name=team_member_repeater', function ($value, $post_id, $field) {
// Ensure the value is an array (repeater data)
if (is_array($value)) {
// Filter the repeater rows based on the specific ACF subfield 'team_member_show_on_cta' being true
$filtered = array_filter($value, function ($row) {
// Check if the subfield 'team_member_show_on_cta' is true
return !empty($row['team_member_show_on_cta']); // Ensure this subfield name matches exactly
});
// Limit the number of filtered results to 5
$limited = array_slice($filtered, 0, 3);
} else {
$limited = $value; // Return unfiltered if not an array
}
// Return the limited and filtered repeater data
return $limited;
}, 10, 3);