Home › Forums › Add-ons › Repeater Field › Sort by key in repeater field
Hi guys,
I can’t figure out how to sort my repeater field by “email”. I use Twig/Timber and would like to sort the data before I loop trough it in the twig file. Any idea? I’ve got name, image, description & email.
{% for person in post.get_field('person') %}
{% for group in person.personalegruppe %}
{% if group.term_id == personalegruppe %}
{{person.name}}
{{person.image}}
...
{% endif %}
{% endfor %}
{% endfor %}
I can’t tell you how to do this in twig/timber. There have been a few questions here about this template system, and they get few responses. The best that I can give you is a link to the developer’s “How To” on sorting repeater fields https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/
You can find other examples on the web that cover it, but like I said, I can’t tell you how you’d do this inside of your template. You would need to build a custom acf/load_value filter in PHP.
That might be the way I’d go if I had to do it as well. Quite honestly the whole array_multisort thing hurts my head.
For the future for anyone that finds this, I did find an alternate solution, you could also try using usort() http://php.net/manual/en/function.usort.php.
// 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;
}
}
Here’s an answer on sorting a repeater field in Timber/Twig: https://stackoverflow.com/questions/49805943/sorting-a-repeater-field-from-advanced-custom-fields-in-php-and-timber-twig/49861014
The topic ‘Sort by key in repeater field’ is closed to new replies.
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.