Hugo

Nested where function doesn't work with ".Params.categories"

{{ range first 3 (where (where .Pages "Section" "post" ) ".Params.categories" "not in" "news") }}
{{ .Render "list_latest" }}
{{ end }}

I would like to use nested where function to retrieve the first three articles, excluding certain categories in the post.
But, this nested where function is not actually working.

Does where function not support operators “in” and “not in”?

Hi,

It does. From the docs: https://gohugo.io/functions/where/

in

true if a given field value is included in a matching value; a matching value must be an array or a slice

In your case, "news" is neither an array nor a slice. Also, the given field value here is ".Params.categories", which (I’m assuming) is an array.

What you probably want instead is

Something like (untested)

{{ $posts := (where .Pages "Section" "post" )  }}
{{ $news := (where (where .Pages "Section" "post" ) ".Params.categories" "intersect" (slice "news") ) }}

{{ $notnews := $posts | complement $news | first 3 }}

slice "news" in this case turns "news", a string, to ["news"], an array containing "news"

Then you can {{ range first 3 $notnews }} .

1 Like

I truly appreciate your quick response!
I was able to realize this function!