Home › Forums › Add-ons › Repeater Field › 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?
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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.