Files
hugo/content/en/functions/collections/Where.md
T
Bjørn Erik Pedersen c23d97904f Squashed 'docs/' changes from 0755fb534d..1f8ddb8a52
1f8ddb8a52 content: clarify resources front matter key descriptions
e064ab8528 content: Add deprecation badges to module config page
727ca5563a github: Add push trigger to lint workflow
64dd5c9886 content: Fix typo
c5bc6b6515 github: Fix lint workflow
faec0c3a0a github: Combine linting actions into a single workflow
06112aeaf2 theme: Miscellaneous template edits
75d4902270 theme: Format templates with gotmplfmt
fec2e2a67e content: Document that the language code in a file name must be lowercase
9dbd841ba6 content: Document the src attribute in the Page Resources metadata reference
fd3ffef985 content: Fix "build from source" instructions for Windows
af4c9cd4d7 content: Miscellaneous edits
408d8b2f0a content: Miscellaneous edits
e8804afe6e content: Fix typo
d98276be30 content: Updates for v0.161.0
01b1f8fa12 content: Note merge limitation for slice configuration values
d2b18f0c8d content: Document page matcher usage for cascading values
45e5bd9ab3 content: Update Cloudflare Worker host/deploy guide
b83726b89a content: Document fallback rendering for fenced code blocks
8f1eeb42bc content: Update reference for source code shortcode
e8da56303b content: Add gotmplfmt to list of VS Code extensions
950fabbfd6 content: Update FAQ on feature availability error
6411146d24 content: Update quick start guide
38cc39fd51 content: Add Hugo Shortcodes to list of VS Code extensions
72d98b107b content: Misc updates to get validators to pass
9fb0e1ca35 Add a paragraph about sec boundaries
e6abf5644f content: Improve syntax highlighting documentation
c06193bd1a content: Update go-i18n package reference
ce58fef945 Hugo 0.161.1
c7e0f63385 content: Fix package references
7f15fb3bf9 data: Regen docshelper
7483d53b55 Update HUGO_VERSION to 0.161.0
c4abcdb45f security: Add a bullet point about "pragmatic defaults"
3cd7492862 content: Improve explanation of mount removal in module configurations
4099f07bb9 content: Update GitHub Pages workflow example
a6c9853a58 content: Fix typo
e6f79a938b Update netlify.toml
abda3d6659 content: Update Action versions in GitHub Pages workflow example
55dd288fa9 content: Add GitCMS to front-ends tools list
21081f6d49 content: Remove outdated new-in badges
b2ec263884 content: Update version references
825e0b8ea9 One more CSS var adjustment
85f95a899b Adjust css.Build var docs a little
df48288002 content: Updates for v0.160.0
a82a9b9797 Update HUGO_VERSION to 0.160.0
1155747dc4 content: Improve CSS processing feature description
f6ce893974 content: Add css.Build to features
67b8ed1198 content: Fix typos
0f62a67863 content: Fix typo
dbb42aed4a content: Document the deploy edition
549f30f933 content: De-emphasize references to the extended edition
8f5c9782d4 content: Add Pages CMS to front-ends documentation
b2bfc3af48 Update HUGO_VERSION to 0.159.2
3793156fc5 content: Fix typos
bacd4824ef content: Specify function namespace in example
7f2dc0d40a Regen docs.yml
65a851f731 Update HUGO_VERSION to 0.159.1
ce05fe3fc0 content: Adjust variable references in build script examples
8a04f9fe64 content: Improve hosting build script examples
67962ce05c content: Link to Codeberg Pages 404 handling
fd248f57ed content: Identify esbuild as the foundation for build functions
62f02879fd content: Remove outdated content
553c407f9e content: Miscellaneous corrections
77e2cad088 content: Add new-in badge for usePackageJSON
0746e1e621 Add a page on using npm dependencies in Hugo Modules
8824850f5c Update HUGO_VERSION to 0.159.0

git-subtree-dir: docs
git-subtree-split: 1f8ddb8a5230518f07c50b4b03cba3cae21081c4
2026-05-21 12:22:48 +02:00

12 KiB

title, description, categories, keywords, params, aliases
title description categories keywords params aliases
collections.Where Returns a slice by filtering the given slice based on a key, operator, and value.
functions_and_methods
aliases returnType signatures
where
[]any
collections.Where SLICE KEY [OPERATOR] VALUE
/functions/where

The where function returns the given slice, removing elements that do not satisfy the comparison condition. The comparison condition is composed of the KEY, OPERATOR, and VALUE arguments:

collections.Where SLICE KEY [OPERATOR] VALUE
                        --------------------
                        comparison condition

Hugo will test for equality if you do not provide an OPERATOR argument. For example:

{{ $pages := where .Site.RegularPages "Section" "books" }}
{{ $books := where hugo.Data.books "genres" "suspense" }}

Arguments

The where function takes three or four arguments. The OPERATOR argument is optional.

SLICE
([]any) A page collection or a slice of maps.
KEY
(string) The key of the page or map value to compare with VALUE. With page collections, commonly used comparison keys are Section, Type, and Params. To compare with a member of the page Params map, chain the subkey as shown below:
{{ $result := where .Site.RegularPages "Params.foo" "bar" }}
OPERATOR
(string) The logical comparison operator.
VALUE
(any) The value with which to compare. The values to compare must have comparable data types. For example:
Comparison Result
"123" "eq" "123" true
"123" "eq" 123 false
false "eq" "false" false
false "eq" false true

When one or both of the values to compare is a slice, use the in, not in, or intersect operators as described below.

Operators

Use any of the following logical operators:

=, ==, eq
(bool) Reports whether the given field value is equal to VALUE.
!=, <>, ne
(bool) Reports whether the given field value is not equal to VALUE.
>=, ge
(bool) Reports whether the given field value is greater than or equal to VALUE.
>, gt
true Reports whether the given field value is greater than VALUE.
<=, le
(bool) Reports whether the given field value is less than or equal to VALUE.
<, lt
(bool) Reports whether the given field value is less than VALUE.
in
(bool) Reports whether the given field value is a member of VALUE. Compare string to slice, or string to string. See details.
not in
(bool) Reports whether the given field value is not a member of VALUE. Compare string to slice, or string to string. See details.
intersect
(bool) Reports whether the given field value (a slice) contains one or more elements in common with VALUE. See details.
like
(bool) Reports whether the given field value matches the regular expression specified in VALUE. Use the like operator to compare string values. The like operator returns false when comparing other data types to the regular expression.

Note

The examples below perform comparisons within a page collection, but the same comparisons are applicable to a slice of maps.

String comparison

Compare the value of the given field to a string:

{{ $pages := where .Site.RegularPages "Section" "eq" "books" }}
{{ $pages := where .Site.RegularPages "Section" "ne" "books" }}

Numeric comparison

Compare the value of the given field to an int or float:

{{ $books := where site.RegularPages "Section" "eq" "books" }}

{{ $pages := where $books "Params.price" "eq" 42 }}
{{ $pages := where $books "Params.price" "ne" 42.67 }}
{{ $pages := where $books "Params.price" "ge" 42 }}
{{ $pages := where $books "Params.price" "gt" 42.67 }}
{{ $pages := where $books "Params.price" "le" 42 }}
{{ $pages := where $books "Params.price" "lt" 42.67 }}

Boolean comparison

Compare the value of the given field to a bool:

{{ $books := where site.RegularPages "Section" "eq" "books" }}

{{ $pages := where $books "Params.fiction" "eq" true }}
{{ $pages := where $books "Params.fiction" "eq" false }}
{{ $pages := where $books "Params.fiction" "ne" true }}
{{ $pages := where $books "Params.fiction" "ne" false }}

Member comparison

Compare a scalar to a slice.

For example, to return a slice of pages where the color page parameter is either "red" or "yellow":

{{ $fruit := where site.RegularPages "Section" "eq" "fruit" }}

{{ $colors := slice "red" "yellow" }}
{{ $pages := where $fruit "Params.color" "in" $colors }}

To return a slice of pages where the color page parameter is neither red nor yellow:

{{ $fruit := where site.RegularPages "Section" "eq" "fruit" }}

{{ $colors := slice "red" "yellow" }}
{{ $pages := where $fruit "Params.color" "not in" $colors }}

Intersection comparison

Compare a slice to a slice, returning elements with common values. This is frequently used when comparing taxonomy terms.

For example, to return a slice of pages where any of the terms in the genres taxonomy are "suspense" or "romance":

{{ $books := where site.RegularPages "Section" "eq" "books" }}

{{ $genres := slice "suspense" "romance" }}
{{ $pages := where $books "Params.genres" "intersect" $genres }}

Regular expression comparison

To return a slice of pages where the author page parameter begins with either "victor" or "Victor":

{{ $pages := where .Site.RegularPages "Params.author" "like" `(?i)^victor` }}

{{% include "/_common/functions/regular-expressions.md" %}}

Note

Use the like operator to compare string values. Comparing other data types will result in an empty slice.

Date comparison

Predefined dates

There are four predefined front matter dates: date, [publishDate], lastmod, and [expiryDate]. Regardless of the front matter data format (TOML, YAML, or JSON) these are time.Time values, allowing precise comparisons.

For example, to return a slice of pages that were created before the current year:

{{ $startOfYear := time.AsTime (printf "%d-01-01" now.Year) }}
{{ $pages := where .Site.RegularPages "Date" "lt" $startOfYear }}

Custom dates

With custom front matter dates, the comparison depends on the front matter data format (TOML, YAML, or JSON).

Note

Using TOML for pages with custom front matter dates enables precise date comparisons.

With TOML, date values are first-class citizens. TOML has a date data type while JSON and YAML do not. If you quote a TOML date, it is a string. If you do not quote a TOML date value, it is time.Time value, enabling precise comparisons.

In the TOML example below, note that the event date is not quoted.

+++
title = '2024 User Conference"
eventDate = 2024-04-01
+++

To return a slice of future events:

{{ $events := where .Site.RegularPages "Type" "events" }}
{{ $futureEvents := where $events "Params.eventDate" "gt" now }}

When working with YAML or JSON, or quoted TOML values, custom dates are strings; you cannot compare them with time.Time values. String comparisons may be possible if the custom date layout is consistent from one page to the next. To be safe, filter the pages by ranging over the slice:

{{ $events := where .Site.RegularPages "Type" "events" }}
{{ $futureEvents := slice }}
{{ range $events }}
  {{ if gt (time.AsTime .Params.eventDate) now }}
    {{ $futureEvents = $futureEvents | append . }}
  {{ end }}
{{ end }}

Nil comparison

To return a slice of pages where the "color" parameter is present in front matter, compare to nil:

{{ $pages := where .Site.RegularPages "Params.color" "ne" nil }}

To return a slice of pages where the "color" parameter is not present in front matter, compare to nil:

{{ $pages := where .Site.RegularPages "Params.color" "eq" nil }}

In both examples above, note that nil is not quoted.

Nested comparison

These are equivalent:

{{ $pages := where .Site.RegularPages "Type" "tutorials" }}
{{ $pages = where $pages "Params.level" "eq" "beginner" }}
{{ $pages := where (where .Site.RegularPages "Type" "tutorials") "Params.level" "eq" "beginner" }}

Portable section comparison

Useful for theme authors, avoid hardcoding section names by using the where function with the MainSections method on a Site object.

{{ $pages := where .Site.RegularPages "Section" "in" .Site.MainSections }}

With this construct, a theme author can instruct users to specify their main sections in their project configuration:

{{< code-toggle file=hugo >}} mainSections = ['blog','galleries'] {{< /code-toggle >}}

If mainSections is not defined in your project configuration, the MainSections method returns a slice with one element---the top-level section with the most pages.

Boolean/undefined comparison

Consider this project structure:

content/
├── posts/
│   ├── _index.md
│   ├── post-1.md  <-- front matter: exclude = false
│   ├── post-2.md  <-- front matter: exclude = true
│   └── post-3.md  <-- front matter: exclude not defined
└── _index.md

The first two pages have an "exclude" field in front matter, but the last page does not. When testing for equality, the third page is excluded from the result. When testing for inequality, the third page is included in the result.

Equality test

This template:

<ul>
  {{ range where .Site.RegularPages "Params.exclude" "eq" false }}
    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  {{ end }}
</ul>

Is rendered to:

<ul>
  <li><a href="/posts/post-1/">Post 1</a></li>
</ul>

This template:

<ul>
  {{ range where .Site.RegularPages "Params.exclude" "eq" true }}
    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  {{ end }}
</ul>

Is rendered to:

<ul>  
  <li><a href="/posts/post-2/">Post 2</a></li>
</ul>

Inequality test

This template:

<ul>
  {{ range where .Site.RegularPages "Params.exclude" "ne" false }}
    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  {{ end }}
</ul>

Is rendered to:

<ul>
  <li><a href="/posts/post-2/">Post 2</a></li>
  <li><a href="/posts/post-3/">Post 3</a></li>
</ul>

This template:

<ul>
  {{ range where .Site.RegularPages "Params.exclude" "ne" true }}
    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  {{ end }}
</ul>

Is rendered to:

<ul>
  <li><a href="/posts/post-1/">Post 1</a></li>
  <li><a href="/posts/post-3/">Post 3</a></li>
</ul>

To exclude a page with an undefined field from a boolean inequality test:

  1. Create a slice using a boolean comparison
  2. Create a slice using a nil comparison
  3. Subtract the second slice from the first slice using the collections.Complement function.

This template:

{{ $p1 := where .Site.RegularPages "Params.exclude" "ne" true }}
{{ $p2 := where .Site.RegularPages "Params.exclude" "eq" nil }}
<ul>
  {{ range $p1 | complement $p2 }}
    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  {{ end }}
</ul>

Is rendered to:

<ul>
  <li><a href="/posts/post-1/">Post 1</a></li>
</ul>

This template:

{{ $p1 := where .Site.RegularPages "Params.exclude" "ne" false }}
{{ $p2 := where .Site.RegularPages "Params.exclude" "eq" nil }}
<ul>
  {{ range $p1 | complement $p2 }}
    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
  {{ end }}
</ul>

Is rendered to:

<ul>
  <li><a href="/posts/post-1/">Post 2</a></li>
</ul>