Post sorting issues in WordPress using Twig/Timber

Viewed 120

So we have a custom Wordpress Pet Site in which we sort pet posts by the ACF field of litter_born and it all works fine until no posts show and then it will just throw a page error. Here is the code we are using:

{% for post in posts | sort((a, b) => b.litter_born <=> a.litter_born) %}
//pulling in post data
{% endfor %}

Which works fine until no posts show then it throws and error:

enter image description here

1 Answers

Wow, that's quite a bit of logic you got there in that loop filter! I think it's as simple as adding a conditional to ensure that posts has a length. Based on the error, sort requires a Traversable and doesn't itself have logic to gracefully handle a NULL

Related