Logical "and" in "where" query

Hi,

I’d like to write a simple where query with the shape where A and B.

Ideally it would look something like this:

{{ range where .Data.Pages "Foo" "bar" and ".Draft" false }}

Instead I’m currently nesting two where statements:

{{ range where (where .Data.Pages "Foo" "bar") ".Draft" false }}

Is there syntax to do this, or is it not currently possible? Logical operators were not listed in the operators table at http://gohugo.io/templates/functions#where.

try something like:

{{ range where (and (.Data.Pages "Foo" "bar") (".Draft" false)) }}

The logical operators are listed in the primer with logic: https://gohugo.io/templates/go-templates#conditionals

2 Likes

I’m having some trouble getting this to work. Based on justrjlewis’ comment and the documentation, I’m inferring that this should be the syntax:

where (and (COLLECTION KEY_1 [OPERATOR_1] MATCH_1) (KEY_2 [OPERATOR_2] MATCH_2))

Is that correct? Because I seem to be getting errors like “wrong number of args for where: want at least 2 got 1”.

(Sorry to dig up this old thread, but I feel like getting clarification on this would also benefit future visitors who found this through a search engine like I did.)

Sorry to dig up this old thread, but I feel like getting clarification on this would also benefit future visitors who found this through a search engine like I did.

How about opening up a new post with your issue then? Also having a syntactical correct sample (yes, what you wrote is right) does not help to check your specific case. A “real” sample that throws the also completely posted error is the least you should add.

My guess into the black-box you hand us is, that you probably lost count of opening and closing brackets somewhere along the line.

1 Like