I need to sort a blog list result based on publish date and a custom module for updated date.
SOLVE
I need to sort a blog list based in published date and custom module for updated date. But after the For Loop for the widget the data for the custom date gets cleared. Please see the markup comments in the code.
<div class="blog-filter__content" id="blog-posts-container">
{% for content in contents %}
{% set effective_date = content.publish_date %}
{% if content.widgets %}
{% for widget in content.widgets %}
{% if widget.body.show_custom_update_date %}
{% set effective_date = widget.body.updated_date|datetimeformat('%Y-%m-%d %H:%M:%S') %}
{{effective_date}}<!-- includes updated_date -->
{% endif %}
{% endfor %}
{% endif %}
{% set content.effective_date = effective_date %}
{{effective_date}}<!-- missing updated_date missing -->
{% endfor %}
<!-- {% set sorted_contents = sorted_contents | sort('effective_date', true) %} -->
{% set sorted_contents = content|sort(attribute='effective_date', reverse=True) %}
{% for content in sorted_contents %}
<div class="blog-filter__item blog-hidden">
<!-- list goes here -->
</div>
{% endfor %}
</div>
Any variables defined within loops are limited to the scope of that loop and cannot be called from outside of the loop. So, you have a for-loop inside another for-loop, and both of them set the variable effective_date and its two different variables in different scopes.
Any variables defined within loops are limited to the scope of that loop and cannot be called from outside of the loop. So, you have a for-loop inside another for-loop, and both of them set the variable effective_date and its two different variables in different scopes.