Home › Forums › ACF PRO › Sorting repeater fields by date › Reply To: Sorting repeater fields by date
What is the name of the repeater field and what is the name of the field you want to sort on?
// get the entire repeater in a multidimensional array
$repeater = get_field('YOUR-REPEATER');
usort($repeater, 'SORT_REPEATER_BY_YOUR_SUB_FIELD');
// usort function
function SORT_REPEATER_BY_YOUR_SUB_FIELD($a, $b) {
if ($a['YOUR-SUB-FIELD'] == $b['YOUR-SUB-FIELD']) {
return 0;
} elseif ($a['YOUR-SUB-FIELD'] < $b['YOUR-SUB-FIELD']) {
return -1;
} else {
return 1;
}
}
Once you’ve done the above then you loop over the array, in order to do this sorting you cannot use the acf have_rows() loop
foreach ($repeater as $row) {
// show items from this row
// example, to echo one of the sub fields
echo $row['SUB-FILED-NAME'];
}
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.