where - Liquid Filter Reference - 0.10.11

Filter Name where
Group Arrays
Version 0.10.11

Description

Creates an array including only the objects with a given property value, or any truthy value by default.

Examples

In this example, assume you have a list of pages and you want to show your help pages separately. Using where, you can create an array containing only the pages that have a “category” of “help”:
All articles:
{% for article in articles -%}
- {{ article.title }}
{% endfor -%}

{% assign help_articles = articles | where: "category", "help" %}

Help articles:
{% for article in help_articles -%}
- {{ article.title }}
{% endfor %}
All articles:
- Support
- Stock News
- FAQs
- About Us
- Troubleshooting
- Setup Guide

Help articles:
- Support
- FAQs
- Troubleshooting
- Setup Guide
Say instead you have a list of articles and you only want to show those that are published. You can use where with a property name but no target value to include all articles with a truthy “published?” value:
All articles:
{% for article in articles -%}
- {{ article.title }}
{% endfor -%}

{% assign published_articles = articles | where: "published?" %}

Published articles:
{% for article in published_articles -%}
- {{ article.title }}
{% endfor %}
All articles:
- Support
- Stock News
- FAQs
- About Us
- Troubleshooting
- Setup Guide

Published articles:
- Support
- FAQs

Back to Filter List

Back to Liquid Reference for 0.10.11

 

 
Back to Documentation