17

I try to do things like this in my post:

<ul class="articles-list">
  {% for post in site.posts | where:'group', post.group %}
    <div data-scroll-reveal="enter ease 0">
      {% include article-snippet.html %}
    </div>
  {% endfor %}
</ul>

but it loops through all my collection instead the only loops posts with special group.

Humayun Shabbir
  • 2,961
  • 4
  • 20
  • 33
Anton Shuvalov
  • 3,560
  • 3
  • 16
  • 20

2 Answers2

26

You cannot use where filter in a loop.

But you can do :

{% assign posts = site.posts | where:'group', post.group %}

<ul class="articles-list">
  {% for post in posts %}
    <div data-scroll-reveal="enter ease 0">
      {% include article-snippet.html %}
    </div>
  {% endfor %}
</ul>
David Jacquel
  • 51,670
  • 6
  • 121
  • 147
4

According to liquid documentation about filters they should be used inside output tags {{ and }}.

You can maybe try an if statement:

{% for post in site.posts %}
    {% if post.group == 'group' %}
        <div data-scroll-reveal="enter ease 0">
            {% include article-snippet.html %}
        </div>
    {% endif %}
{% endfor %}

Also you should use the where filter a bit differently. Something like this:

{{ site.members | where:"graduation_year","2014" }}

This says select site members whose graduation_year is 2014. You don't need to specify that it should filter members, the first part implicitly states that.

Roland Studer
  • 4,315
  • 3
  • 27
  • 43
Mitja Bezenšek
  • 2,503
  • 1
  • 14
  • 17