where

Table of contents

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)

{{ site.posts | where: "title", "Only this title" }}

Returns all posts that have ‘Only this title’ as the title.

{{ site.posts | where: "categories", category, "contains" }}
Input

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.

{{ site.posts | where: "published_at", 'March 25 2018', "<" }}
Returns all posts that were published before 25 March 2018.
{{ site.posts | where: "published_at", 'now', "<" }}
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

{{ site.posts | where: "title", "published_at", 'March 25 2018', "title", "Only this title" }}

returns the same result as

{{ site.posts | where: "title", "published_at", 'March 25 2018', "==", "title", "Only this title", "==" }}

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:

{{ site.posts | where: "title", "Only this title" | where: "published_at", 'March" }} 

Need help?

Do you have any question which is not answered in this knowledge base? Contact us. We are here to help you.