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.
Thanks John, I gave up and solved it with Javascript.
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;
}
}
Wow, thats awesome, I’ll give it a try. Thanks man.