Support

Account

Home Forums Add-ons Repeater Field Sort Repeater Field by Subfield in Admin (Backend)

Solving

Sort Repeater Field by Subfield in Admin (Backend)

  • I’m trying to sort a Repeater field in the backend, following the documentation in the “Advanced” section, and using the acf/load_value filter.

    My issue is that the $value returned is just the number of rows (I have 3 rows, so it returns string(1) “3”), and not an array, which is what the documentation seems to suggest. There isn’t any sub-field data/object revealed here, so the foreach() doesn’t get supplied a proper argument (it’s not an array).

    My code is as follows:

    function my_acf_load_value( $value, $post_id, $field ) {
    // vars
    $order = array();
    
    // populate order
    foreach( $value as $i => $row ) {
    
    $order[ $i ] = $row['field_56e832f858a11'];
    
    }
    
    // multisort
    array_multisort( $order, SORT_DESC, $value );
    
    // return
    return $value;
    }
    add_filter('acf/load_value/name=fall_term', 'my_acf_load_value', 10, 3);

    I will eventually want to sort by DatePicker, but for now am just trying to get it to sort by a Number.

    Any idea what I am missing here?

  • Hi @youllbejustfine

    I’m afraid I couldn’t reproduce the issue on my end. The $value variable will return 0 if there’s no entries in the repeater. For such case, you need to check it first like this:

    if( empty($value) ) {
        return $value;
    }

    Did you create the repeater field from other field types such as text? Do you have a same field key or name assigned to the page? Could you please share the JSON or XML export of your field group?

    Thanks.

  • I just ran into a similar problem and concluded, without any evidence, that it’s because the repeater field is a subfield of another repeater field. Perhaps this is the case here as well.

  • Actually it seems like the parent repeater field simply returns the number of subfields as well, no idea why it doesn’t work as described. I’m using ACF v4.4.8 w/ Repeater field add-on v1.1.1.

  • Hi @marcguay

    I’m afraid the acf/load_value filter will always return the number of total row instead of the array for the Repeater add-on and ACF free version.

    Because you have the Repeater add-on, you should be able to claim the ACF PRO Personal license. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/upgrading-v4-v5/#how-to update. I suggest you use the PRO version instead.

    If you still want to use the Repeater add-on, then please use the basic code instead of the advanced code.

    I hope this helps 🙂

  • Thanks James. Perhaps a note in the docs would be helpful to others in the same situation.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Sort Repeater Field by Subfield in Admin (Backend)’ is closed to new replies.