where
Select all objects in an array where the attribute (first argument) returns a certain value (second argument). The default comparison operator is ==
(equal to), but you can pass another operator as the third argument. You can choose from:
==
(equal to)!=
(not equal to)>
(greater than)<
(less than)>=
(greater than or equal to)<=
(less than or equal to)contains
(contains substring in String or a certain object in Array)
Input:
{{ site.posts | where: "title", "Only this title" }}
Returns all posts that have ‘Only this title’ as the title.
Input:
{{ site.posts | where: "categories", category, "contains" }}
Returns all posts that have the passed category as category. In this case post.categories
returns an array, so the contains
operator must be used.
You can also compare by date and time, which gets parsed automatically when the passed attribute name returns a date. The words now
or today
are parsed as the current time.
Input:
{{ site.posts | where: "published_at", 'March 25 2018', "<" }}
Returns all posts that were published before 25 March 2018.
Input:
Returns all posts that were published in the past.
Logical operators, and/or
You can pass more than three arguments to the where filter, to use multiple rules to compare in the selection. Every third argument is the comparison operator and is optional. E.g. passing"published_at", 'March 25 2018', "title", "Only this title"
returns the same result as"published_at", 'March 25 2018', "==", "title", "Only this title", "=="
.
Input:
Returns every post that has “Only this title” as the title OR is created before March 25 2018.
If you want to have only the posts that have the title AND are created before March 25 2018, you can chain the filter like this:
Input: