Possible to use `where` with the WordCount field?

I’m trying to find all pages below a certain word count and trying to determine whether it’s possible to use where to do so.

I’ve got logic that pulls all pages that looks like this:

{{ $pages := .Site.GetPage "/some-pages" }}
{{ $pages := where $pages.Resources "Draft" false }}

This correctly works.

Where the directory structure is:

some-pages/
   index.md
   page-1.md
   page-2.md

I’m then trying to use where on .WordCount which is available in each subpage ie {{ (index $pages 0).WordCount }}.

What I’ve tried are things like:

{{ $wc := 100 }}
{{ where $pages "WordCount" "le" $wc }}
{{ where $pages ".WordCount" "le" $wc }}
// other variations

Is it possible to use where in this fashion?

EDIT: This does work, but requires a new slice and looping, my question about where still stands:

{{ $pageSet := .Site.GetPage "/some-pages" }}
{{ $pageSet := where $pageSet.Resources "Draft" false }}
{{ $pages := slice }}
{{ range $pageSet }}
    {{ if lt .WordCount 100 }}
        {{ $pages = $pages | append . }}
    {{ end }}
{{ end }}

Fields such as .WordCount, .FuzzyWordCount, and .Len are not known until the page content is evaluated. You could use these fields in a where clause with v0.110.0 and earlier, but not with v0.111.0 and later. See https://github.com/gohugoio/hugo/issues/11234.

For now, evaluate within the loop.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.