Answers.Body: <p>Guessing the issue is that your <code>whereParams</code> variable is actually a string, brackets and all – not an array. So, you'll just need to make sure that it's an actual array instead.</p>
  <p>The good news is that you're quite close, just by doing <code>{% set lsoas = uentry.selectedLsoas | split(&quot;\n&quot;) %}</code> – at that point the <code>lsoas</code> variable is actually an array already, so in theory you should just be able to use that variable as the <code>whereParams</code> directly.</p>
  <p>Something like this should work – note the <code>|filter</code> appended after the <code>|split</code>, this will remove any empty values from the array, and the <code>|map(ons =&gt; ons|trim)</code> part removes leading/trailing whitespace from each value:</p>
  <pre><code>{% set lsoas = uentry.selectedLsoas | split(&quot;\n&quot;) | filter | map(ons =&gt; ons|trim) %}
  
  {% set mylsoas =  craft.entries
      .section('imdData')
      .orWhere(['in', 'content.field_lsoacode', lsoas])
      .all() %}
  </code></pre>
  <p>Finally, I'm not sure why you need to use <code>orWhere()</code> (or simply <code>where()</code>) in this case – you can probably just do this, which is a lot simpler:</p>
  <pre><code>{% set mylsoas =  craft.entries
      .section('imdData')
      .lsoacode(lsoas)
      .all() %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-20T10:43:48.047Z
Answers.Text: Thanks for detailed response. I thought i was overcomplicating it - you are right, i didn't need where() at all.
Answers.UserDisplayName: joomkit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-19T20:31:21.517Z
Answers.LastActivityDate: 2022-02-19T20:44:12.577Z
Answers.LastEditDate: 2022-02-19T20:44:12.577Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.Body: <p>You may be overthinking this! It's pretty rare that you'll need to reach directly for the ActiveQuery <code>where</code>, <code>orWhere</code>, and <code>andWhere</code> methods—unless I've misunderstood what kind of results you're going for.</p>
  <blockquote>
  <p>There are couple unrelated issues with the sample code that I think are worth addressing, but we're going to sidestep them with the solution.</p>
  <p>Broadly, though, it looks like what you're trying to do is replicate Twig <em>syntax</em> (<code>&quot;['one', 'two', 'three']&quot;</code>), rather than build up an <em>array</em> (<code>['one', 'two', three']</code>).</p>
  <p>Documentation for <code>.where()</code>, describing the <a href="https://www.yiiframework.com/doc/api/2.0/yii-db-queryinterface#where()-detail" rel="nofollow noreferrer">range of formats for conditions</a>.</p>
  </blockquote>
  <p>That said, <strong>Craft automatically provides some convenience methods for working with fields</strong>. In your case, I think the default behavior is enough to produce the desired results:</p>
  <pre><code>{% set mylsoas =  craft
    .entries()
    .section('imdData')
    .lsoacode(['E01001667', 'E01001668', 'E01000779'])
    .all() %}
  </code></pre>
  <p>This will produce an <code>andWhere()</code> SQL condition like <code>content.field_lsoacode IN ('E01001667', 'E01001668', 'E01000779')</code>.</p>
  <p>So, if you need to parameterize this:</p>
  <pre><code>{# Template: main.twig #}
  {% set lsoas = uentry.selectedLsoas | split(&quot;\n&quot;) %}
  
  {{ include('lsoa-list', {
    codes: lsoas,
  }) }}
  
  {# Template: lsoa-list.twig #}
  
  {% set mylsoas =  craft
    .entries()
    .section('imdData')
    .lsoacode(codes)
    .all() %}
  </code></pre>
  <p>If you do really need the <code>orWhere()</code> syntax, then we may have to dive deeper into managing arrays in Twig, and check out <a href="https://craftcms.stackexchange.com/questions/38471/get-array-of-titles-instead-of-whole-collection-of-entries-attributes">this question and Moritz's answer</a> about how to dynamically build queries now that field column handles include a randomized suffix.</p>
  <p>The same parameterization seems like it'd still work, though, in this context…</p>
  <pre><code>{# Template: main.twig #}
  {% set lsoas = uentry.selectedLsoas | split(&quot;\n&quot;) %}
  
  {{ include('lsoa-list', {
    codes: lsoas,
  }) }}
  
  {# Template: lsoa-list.twig #}
  
  {% set mylsoas =  craft
    .entries()
    .section('imdData')
    .orWhere(['in', 'content.field_lsoacode', codes])
    .all() %}
  </code></pre>
  <p>…because <code>codes</code> is already an array in the <code>in</code> operator format expected by <code>where()</code>.</p>
  <p>Just to reiterate—unless there's something <em>really</em> special about to your query that requires direct use of <code>orWhere()</code>, I would avoid it!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-20T10:45:51.973Z
Answers.Text: Thanks for detailed response August. I was overcomplicating it -and not educated enough in the way of the twig!
Answers.UserDisplayName: joomkit
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-19T20:30:08.467Z
Answers.LastActivityDate: 2022-02-19T20:30:08.467Z
Answers.UserDisplayName: August Miller
Body: <p>I have a text field that has three values on new lines.</p>
  <pre><code>E01001667
  E01001668
  E01000779
  </code></pre>
  <p>I want to use these for a query that looks like this:</p>
  <pre><code>{% set mylsoas =  craft.entries.
  section('imdData')
  .orWhere(['in', 'content.field_lsoacode', ['E01001667','E01001668','E01000779']])
  .all() %}
  </code></pre>
  <p>The above query works and returns the records I want when i add the values directly.
  However I need to build the where params dynamically from the text field values so I am doing this:</p>
  <pre><code>{%  set lsoas = uentry.selectedLsoas | split(&quot;\n&quot;) %}
  {% set whereParams = null %}
  {# trim and join #}
  {% for ons in lsoas %}
      {%  set whereParams = whereParams ~ &quot;'&quot;~ons~&quot;',&quot; %}
  {% endfor %}
  {% set whereParams = &quot;[&quot; ~ whereParams|trim(',', 'right') ~ &quot;]&quot; %}
  </code></pre>
  <p>The output of whereParams on screen works in the query and give me this:</p>
  <pre><code>{{ whereParams }}
  outputs: ['E01001667','E01001668','E01000779'] 
  </code></pre>
  <p>But when i pass the variable in to the query i get a zero results - no errors.</p>
  <pre><code>{% set mylsoas =  craft.entries.
  section('imdData')
  .orWhere(['in', 'content.field_lsoacode', whereParams])
  .all() %}
  </code></pre>
  <p>At a loss to get this working.
  Any ideas welcome:)</p>
  
Tags: <query><elementquery><custom>
Title: Cannot pass where Params to query with Twig variable
ThreadText: # Cannot pass where Params to query with Twig variable
  
  (Asked by: username_0 on 2022-02-19)
  
  I have a text field that has three values on new lines.
  
  ```
  E01001667
  E01001668
  E01000779
  
  ```
  
  I want to use these for a query that looks like this:
  
  ```
  {% set mylsoas =  craft.entries.
  section('imdData')
  .orWhere(['in', 'content.field_lsoacode', ['E01001667','E01001668','E01000779']])
  .all() %}
  
  ```
  
  The above query works and returns the records I want when i add the values directly.
  However I need to build the where params dynamically from the text field values so I am doing this:
  
  ```
  {%  set lsoas = uentry.selectedLsoas | split("\n") %}
  {% set whereParams = null %}
  {# trim and join #}
  {% for ons in lsoas %}
      {%  set whereParams = whereParams ~ "'"~ons~"'," %}
  {% endfor %}
  {% set whereParams = "[" ~ whereParams|trim(',', 'right') ~ "]" %}
  
  ```
  
  The output of whereParams on screen works in the query and give me this:
  
  ```
  {{ whereParams }}
  outputs: ['E01001667','E01001668','E01000779'] 
  
  ```
  
  But when i pass the variable in to the query i get a zero results - no errors.
  
  ```
  {% set mylsoas =  craft.entries.
  section('imdData')
  .orWhere(['in', 'content.field_lsoacode', whereParams])
  .all() %}
  
  ```
  
  At a loss to get this working.
  Any ideas welcome:)
  
  **Tags:** query, elementquery, custom
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2022-02-19. Score: 2 | Accepted answer
  
  Guessing the issue is that your `whereParams` variable is actually a string, brackets and all – not an array. So, you'll just need to make sure that it's an actual array instead.
  
  The good news is that you're quite close, just by doing `{% set lsoas = uentry.selectedLsoas | split("\n") %}` – at that point the `lsoas` variable is actually an array already, so in theory you should just be able to use that variable as the `whereParams` directly.
  
  Something like this should work – note the `|filter` appended after the `|split`, this will remove any empty values from the array, and the `|map(ons => ons|trim)` part removes leading/trailing whitespace from each value:
  
  ```
  {% set lsoas = uentry.selectedLsoas | split("\n") | filter | map(ons => ons|trim) %}
  
  {% set mylsoas =  craft.entries
      .section('imdData')
      .orWhere(['in', 'content.field_lsoacode', lsoas])
      .all() %}
  
  ```
  
  Finally, I'm not sure why you need to use `orWhere()` (or simply `where()`) in this case – you can probably just do this, which is a lot simpler:
  
  ```
  {% set mylsoas =  craft.entries
      .section('imdData')
      .lsoacode(lsoas)
      .all() %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for detailed response. I thought i was overcomplicating it - you are right, i didn't need where() at all.  
  
  ---
  
  ### Answer by username_2 on 2022-02-19. Score: 3
  
  You may be overthinking this! It's pretty rare that you'll need to reach directly for the ActiveQuery `where`, `orWhere`, and `andWhere` methods—unless I've misunderstood what kind of results you're going for.
  
  > There are couple unrelated issues with the sample code that I think are worth addressing, but we're going to sidestep them with the solution.
  >
  > Broadly, though, it looks like what you're trying to do is replicate Twig *syntax* (`"['one', 'two', 'three']"`), rather than build up an *array* (`['one', 'two', three']`).
  >
  > Documentation for `.where()`, describing the [range of formats for conditions](https://www.yiiframework.com/doc/api/2.0/yii-db-queryinterface#where()-detail).
  
  That said, **Craft automatically provides some convenience methods for working with fields**. In your case, I think the default behavior is enough to produce the desired results:
  
  ```
  {% set mylsoas =  craft
    .entries()
    .section('imdData')
    .lsoacode(['E01001667', 'E01001668', 'E01000779'])
    .all() %}
  
  ```
  
  This will produce an `andWhere()` SQL condition like `content.field_lsoacode IN ('E01001667', 'E01001668', 'E01000779')`.
  
  So, if you need to parameterize this:
  
  ```
  {# Template: main.twig #}
  {% set lsoas = uentry.selectedLsoas | split("\n") %}
  
  {{ include('lsoa-list', {
    codes: lsoas,
  }) }}
  
  {# Template: lsoa-list.twig #}
  
  {% set mylsoas =  craft
    .entries()
    .section('imdData')
    .lsoacode(codes)
    .all() %}
  
  ```
  
  If you do really need the `orWhere()` syntax, then we may have to dive deeper into managing arrays in Twig, and check out [this question and Moritz's answer](https://craftcms.stackexchange.com/questions/38471/get-array-of-titles-instead-of-whole-collection-of-entries-attributes) about how to dynamically build queries now that field column handles include a randomized suffix.
  
  The same parameterization seems like it'd still work, though, in this context…
  
  ```
  {# Template: main.twig #}
  {% set lsoas = uentry.selectedLsoas | split("\n") %}
  
  {{ include('lsoa-list', {
    codes: lsoas,
  }) }}
  
  {# Template: lsoa-list.twig #}
  
  {% set mylsoas =  craft
    .entries()
    .section('imdData')
    .orWhere(['in', 'content.field_lsoacode', codes])
    .all() %}
  
  ```
  
  …because `codes` is already an array in the `in` operator format expected by `where()`.
  
  Just to reiterate—unless there's something *really* special about to your query that requires direct use of `orWhere()`, I would avoid it!
  
  > **Comments:**
  > **username_0**: Thanks for detailed response username_2. I was overcomplicating it -and not educated enough in the way of the twig!  
  
  ---
  
  