1

I have an include which renders my posts in a certain format. I am passing the posts to the include like below:

{% include post-list.html posts=site.posts %}

However, I would like to filter out a certain category before passing it to the include. Does anyone have any ideas how I can achieve this?

dtsn
  • 1,077
  • 1
  • 10
  • 17

1 Answers1

4

You can do it defining a variable containing an array with all the posts not containing a specific category and then passing that to includes, for example to filter posts containing the category jekyll:

  1. We create the array {% assign notjekyllposts = "" | split: "" %}
  2. Loop all posts {% for apost in site.posts %}
  3. Filter those that does not contain the jekyll category adding them to the array:
    {% assign notjekyllposts = notjekyllposts | push: apost %}
  4. Pass the variable to the include tag

Putting it all together:

{% assign notjekyllposts = "" | split: "" %}

{% for apost in site.posts %}
  {% unless apost.categories contains 'jekyll' %}
    {% assign notjekyllposts = notjekyllposts | push: apost %}
  {% endunless %}
{% endfor %}
{% include post-list.html posts=notjekyllposts %}
ashmaroli
  • 5,209
  • 2
  • 14
  • 25
marcanuy
  • 23,118
  • 9
  • 64
  • 113
  • Thanks for the response but I would like to filter out a category. Or remove all posts of a certain category out of the array. – dtsn Jul 24 '17 at 21:33