ACF repeater and sorting by custom field

I have a Repeater Field in the ACF. Inside it is a Data field. Is it possible to sort the repeater by the Data field?

Hey @Adam,

you could use ACF’s native hooks for this (see ACF | Sort a repeater field).

Example implementation (which can and probably needs to be adjusted to your specific setup):

/*
 * 1. Replace "repeater" in the filter name with the name of your repeater field:
 * e.g.: acf/format_value/name=your_repeater_field
 */
add_filter( 'acf/format_value/name=repeater', function( $value, $post_id, $field ) {
    /* 2. Optional condition if you only want to alter the repeater order in specific circumstances
     * e.g.: apply the custom order only on the page with an ID of 58
     */
    if ( is_page( 58 ) ) {
        usort( $value, function( $a, $b ) {
            /*
             * 3. Replace "date" in the strcmp function name with the name of the date sub field in your repeater:
             * e.g.: strcmp( $a['your_date_field'], $b['your_date_field'] )
             */
            return strcmp( $a['date'], $b['date'] );
        } );
    }
    return $value;
}, 10, 3 );

Backend:

CleanShot 2023-04-17 at 15.18.44

Frontend (on page 58):

CleanShot 2023-04-17 at 15.54.15

Hope it helps!

2 Likes

Thx, works like a harm

Hey Adam,

I would have preferred it to work like a charm but that’s ok as well I guess. :slight_smile: Glad I could help!

André

1 Like

Lol what an unfortunate misspelling… ye ye, works perfect. luv u

1 Like

Any chance you could show how I make this descending please?