Enable sorting/ordering of Repeater fields in a Query Loop

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.

It would be great to be able to order the Repeater field group, ideally based on one of the Repeater groups fields ( such as a date ).

BTW I am using ACPT but assume this also applies to other Custom Post/Fields providers.

1 Like

Hi

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 );

Sort1

Replace the element ID hmhovo in the code with your element ID of the repeater or relationship query.

Cheers

Patric

Hey Patric

How could this be updated to check if a true-false field was ticked/present please?

TY

Hi

I think you could just use a condition and check if the ACF true / false field is 0 or 1.

Unless I misunderstood, I don’t think you need any code.

Cheers

Patric

Thanks Patric.

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).

Cheers

Looks like nothing other than a custom function will work for this, at least for me :frowning: 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);