Home › Forums › ACF PRO › Sorting a list using repeater and multi-select field › Reply To: Sorting a list using repeater and multi-select field
Just wanted to make sure, didn’t want to answer the wrong question. This may not be the best way to do this but it’s the way I’d do it because it is the most straight forward. What you need to do is put the values from the repeater into and array that can be sorted.
$books = array();
if (have_rows('books')) {
while (have_rows('books')) {
the_row();
$category = get_sub_field('category');
if (!isset($books[$category])) {
$books[$category] = array();
}
$books[$category][] = get_sub_field('title');
}
}
// sort the categories
// ksort sorts the array by the element index
ksort($books);
// loop through books
foreach ($books as $category => $list) {
echo '<h2>',$category,'</h2'><ul>';
// sort the list
sort($list);
foreach ($list as $book) {
echo '<li>',$book,'</li>';
}
echo '</ul>';
}
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.