Home › Forums › ACF PRO › Sorting repeater fields by date › Reply To: Sorting repeater fields by date
@soph I am not a beginner at PHP and I find array_multisort() overly complex, so don’t feel this is the problem. I find that using usort() is much more straight forward. The idea is that you create your own sorting function and call it with the array of the repeater and then use any of the fields on the row to determine the order. This is a basic example I posted here https://support.advancedcustomfields.com/forums/topic/sort-by-key-in-repeater-field/#post-49954, but I’ll repeat it
// get the entire repeater in a multidimensional array
$repeater = get_field('repeater');
usort($repeater, 'sort_by_email');
// usort function
function sort_by_email($a, $b) {
if ($a['email'] == $b['email']) {
return 0;
} elseif ($a['email'] < $b['email']) {
return -1;
} else {
return 1;
}
}
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.