Answers.Body: <p>My guess would be that this is happening due to lingering parameter values, as it looks like you’re not using <a href="https://craftcms.com/docs/3.x/dev/functions.html#clone" rel="nofollow noreferrer">clone()</a> at all.</p>
  <p>For every place that you are calling <code>entry.programme</code>, change to <code>clone(entry.programme)</code>. For example:</p>
  <pre><code>{% set talks   = clone(entry.programme).type('item').all() %}
  {% set odTalks = clone(entry.programme).type('item').od(':notempty:').all() %}
  </code></pre>
  <p>If you don’t clone the entry query before setting parameters on it and executing it, then whatever parms you set will stay set on it the next time you access the same entry query further on in your template.</p>
  <p>(That is mentioned throughout the docs wherever we demonstrate setting parameters on element query field values, e.g. under <a href="https://craftcms.com/docs/3.x/matrix-fields.html#working-with-matrix-field-data" rel="nofollow noreferrer">Working with Matrix Field Data</a>. But I realize it’s awkward &amp; annoying – see <a href="https://github.com/craftcms/cms/issues/5640" rel="nofollow noreferrer">craftcms/cms#5640</a> for my idea of how to improve this in Craft 4.)</p>
  <p>If adding <code>clone()</code> functions everywhere doesn’t solve this on its own, the next thing I’d do is run the query through <a href="https://docs.craftcms.com/api/v3/craft-db-query.html#method-getrawsql" rel="nofollow noreferrer">getRawSql()</a> and see what that produces:</p>
  <pre><code>&lt;pre&gt;{{ clone(entry.programme).type('item').od(':notempty:').getRawSql() }}&lt;/pre&gt;
  </code></pre>
  <p>You can paste the query that’s output into a MySQL client, and see if there’s anything obviously wrong with it. Or paste it into your question above.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-19T16:04:24.453Z
Answers.Text: Thanks Brad. All very good to know. Will try this.
Answers.UserDisplayName: Jonathan Schofield
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-17T12:35:06.490Z
Answers.LastActivityDate: 2020-08-17T12:35:06.490Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I’m trying to do something, the answer to which should be as simple as <a href="https://craftcms.stackexchange.com/a/26234/3507">searching a Matrix field value</a> but that’s not working for reasons I can’t fathom.</p>
  <h3>Setup</h3>
  <ul>
  <li>I have a Matrix field called <code>programme</code>.</li>
  <li>It includes a block type called <code>item</code>.</li>
  <li>That block type has a field with a handle of <code>od</code>.</li>
  <li>I have a test case entry that has 4 <code>item</code> blocks in it.</li>
  <li><em>All 4 of these blocks</em> have a value in their <code>od</code> field but I need to check for scenarios where some or all of those blocks have that field empty.</li>
  </ul>
  <p>So, I’m querying:</p>
  <pre><code>{% set talks   = entry.programme.type('item').all() %}
  {% set odTalks = entry.programme.type('item').od(':notempty:').all() %}
  </code></pre>
  <p>…and then checking that works with:</p>
  <pre><code>&lt;pre&gt;
      talks: {{ talks|length }}
      odTalks: {{ odTalks|length }}
  &lt;/pre&gt;
  </code></pre>
  <p>…and the result I’m getting is:</p>
  <pre><code>talks: 4
  odTalks: 0
  </code></pre>
  <p>I’ve tried other search values I know should work with the specific entry’s values, including regex strings. I've tried searching other fields in the block, and nothing works.</p>
  <p>What could I be doing wrong?</p>
  <hr />
  <p>Would be a shame to have to do something like this just to compare the lengths of <code>talks</code> and <code>odTalks</code> and act accordingly…</p>
  <pre><code>{% set odTalks = 0 %}
  
  {% for talk in talks %}
      {% if talk.od is not empty %}
          {% set odTalks = odTalks + 1 %}
      {% endif %}
  {% endfor %}
  </code></pre>
  <p>…even though that works and produces the expected:</p>
  <pre><code>talks: 4
  odTalks: 4
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-08-14T13:55:22.367Z
Comments.Text: What type of field is `od` ?
Comments.UserDisplayName: Brandon Kelly
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-08-17T10:42:38.347Z
Comments.Text: Plain text, single line input.
Comments.UserDisplayName: Jonathan Schofield
Tags: <matrix><search><query>
Title: Comparing matrix blocks by type and field value
ThreadText: # Comparing matrix blocks by type and field value
  
  (Asked by: username_0 on 2020-08-14)
  
  I’m trying to do something, the answer to which should be as simple as [searching a Matrix field value](https://craftcms.stackexchange.com/a/26234/3507) but that’s not working for reasons I can’t fathom.
  
  ### Setup
  
  * I have a Matrix field called `programme`.
  * It includes a block type called `item`.
  * That block type has a field with a handle of `od`.
  * I have a test case entry that has 4 `item` blocks in it.
  * *All 4 of these blocks* have a value in their `od` field but I need to check for scenarios where some or all of those blocks have that field empty.
  
  So, I’m querying:
  
  ```
  {% set talks   = entry.programme.type('item').all() %}
  {% set odTalks = entry.programme.type('item').od(':notempty:').all() %}
  
  ```
  
  …and then checking that works with:
  
  ```
  <pre>
      talks: {{ talks|length }}
      odTalks: {{ odTalks|length }}
  </pre>
  
  ```
  
  …and the result I’m getting is:
  
  ```
  talks: 4
  odTalks: 0
  
  ```
  
  I’ve tried other search values I know should work with the specific entry’s values, including regex strings. I've tried searching other fields in the block, and nothing works.
  
  What could I be doing wrong?
  
  ---
  
  Would be a shame to have to do something like this just to compare the lengths of `talks` and `odTalks` and act accordingly…
  
  ```
  {% set odTalks = 0 %}
  
  {% for talk in talks %}
      {% if talk.od is not empty %}
          {% set odTalks = odTalks + 1 %}
      {% endif %}
  {% endfor %}
  
  ```
  
  …even though that works and produces the expected:
  
  ```
  talks: 4
  odTalks: 4
  
  ```
  
  **Tags:** matrix, search, query
  
  > **Comments:**
  > **username_1**: What type of field is `od` ?  
  > **username_0**: Plain text, single line input.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-08-17. Score: 2 | Accepted answer
  
  My guess would be that this is happening due to lingering parameter values, as it looks like you’re not using [clone()](https://craftcms.com/docs/3.x/dev/functions.html#clone) at all.
  
  For every place that you are calling `entry.programme`, change to `clone(entry.programme)`. For example:
  
  ```
  {% set talks   = clone(entry.programme).type('item').all() %}
  {% set odTalks = clone(entry.programme).type('item').od(':notempty:').all() %}
  
  ```
  
  If you don’t clone the entry query before setting parameters on it and executing it, then whatever parms you set will stay set on it the next time you access the same entry query further on in your template.
  
  (That is mentioned throughout the docs wherever we demonstrate setting parameters on element query field values, e.g. under [Working with Matrix Field Data](https://craftcms.com/docs/3.x/matrix-fields.html#working-with-matrix-field-data). But I realize it’s awkward & annoying – see [craftcms/cms#5640](https://github.com/craftcms/cms/issues/5640) for my idea of how to improve this in Craft 4.)
  
  If adding `clone()` functions everywhere doesn’t solve this on its own, the next thing I’d do is run the query through [getRawSql()](https://docs.craftcms.com/api/v3/craft-db-query.html#method-getrawsql) and see what that produces:
  
  ```
  <pre>{{ clone(entry.programme).type('item').od(':notempty:').getRawSql() }}</pre>
  
  ```
  
  You can paste the query that’s output into a MySQL client, and see if there’s anything obviously wrong with it. Or paste it into your question above.
  
  > **Comments:**
  > **username_0**: Thanks Brad. All very good to know. Will try this.  
  
  ---
  
  