<p>Keep in mind objects in PHP are always called by reference not by value thus your <code>anchors</code> query will always return blocks from the type <code>subhead</code> with the field value <code>anchor =1</code> while your matrix query <code>anchorLinks</code> will return blocks of type <code>copy</code> with the field value <code>anchor = 1</code> and <code>anchorLink = 1</code></p>

<p>I'm not sure why you don't fetch the results in one single query as it would be faster and easier in my opinion but you want to create 2 queries you need to either reset the previously set property to <code>null</code> or use the <a href="https://docs.craftcms.com/v3/changes-in-craft-3.html#cloning-element-queries" rel="nofollow noreferrer">clone</a>  function </p>

<pre><code>{% set anchors2 = clone(entry.pageBuilder).type('copy').anchorLink('1').all() %}
{% set anchors1 = clone(entry.pageBuilder).type('subhead').anchor('1').all() %}
</code></pre>

CC BY-SA 4.0
2018-10-26T14:20:45.143Z
Thanks! I somehow missed the changes in Craft 3 that make clone necessary. I would actually prefer to get the results in a single query, but I didn't think it was possible with the two different light switch values. Everything I tried gave me no results.
artmem
CC BY-SA 4.0
2018-10-25T23:04:18.737Z
2018-10-25T23:04:18.737Z
Robin Schambach
<p>I have a (Craft 3) matrix field with two different block types that each have a similar light switch field that I want to use to create anchor links on my page.
I need to be able to test whether any of the fields have the light switch enabled, and also loop through all of those entries.</p>

<p>Orignally, both light switch fields were named 'anchor', but that seemed to cause problems, so now they have two different labels. I'm having trouble combining them into a single object though, or even working with them separately in the same template:</p>

<pre><code>{% set anchors = entry.pageBuilder.type('subhead').anchor('= 1') %}
{% set anchorLinks = entry.pageBuilder.type('copy').anchorLink('= 1') %}

Anchors: {{ anchors|length }}
AnchorLinks: {{ anchorLinks|length }}
</code></pre>

<p>If I use the code above as-is, the value of one of the objects is always 0. But if I use either of those lines indivdually, they return the proper value.</p>

<p>I also tried merging the two objects, but I still run into the same issue of the value of one of the objects being incorrectly set to 0.</p>

<pre><code>{% set anchors2 = entry.pageBuilder.type('copy').anchorLink('1').find() %}
{% set anchors1 = entry.pageBuilder.type('subhead').anchor('1').find() %}

{% set allAnchors = anchors1|merge(anchors2) %}

one {{ anchors1|length }}
two {{ anchors2|length }}
all {{ allAnchors|length }}

{% for anchor in allAnchors %}
    {{ anchor.subhead }}
{% endfor %}
</code></pre>

<p>Is this a bug, or am I doing something wrong?</p>

<matrix><lightswitch><merge><filtering>
filter matrix blocks by multiple light switch value, and then merge them
# filter matrix blocks by multiple light switch value, and then merge them

(Asked by: username_0 on 2018-10-25)

I have a (Craft 3) matrix field with two different block types that each have a similar light switch field that I want to use to create anchor links on my page.
I need to be able to test whether any of the fields have the light switch enabled, and also loop through all of those entries.
Orignally, both light switch fields were named 'anchor', but that seemed to cause problems, so now they have two different labels. I'm having trouble combining them into a single object though, or even working with them separately in the same template:
{% set anchors = entry.pageBuilder.type('subhead').anchor('= 1') %}
{% set anchorLinks = entry.pageBuilder.type('copy').anchorLink('= 1') %}

Anchors: {{ anchors|length }}
AnchorLinks: {{ anchorLinks|length }}

If I use the code above as-is, the value of one of the objects is always 0. But if I use either of those lines indivdually, they return the proper value.
I also tried merging the two objects, but I still run into the same issue of the value of one of the objects being incorrectly set to 0.
{% set anchors2 = entry.pageBuilder.type('copy').anchorLink('1').find() %}
{% set anchors1 = entry.pageBuilder.type('subhead').anchor('1').find() %}

{% set allAnchors = anchors1|merge(anchors2) %}

one {{ anchors1|length }}
two {{ anchors2|length }}
all {{ allAnchors|length }}

{% for anchor in allAnchors %}
    {{ anchor.subhead }}
{% endfor %}

Is this a bug, or am I doing something wrong?

**Tags:** matrix, lightswitch, merge, filtering

---

## 1 answers

### Answer by username_1 on 2018-10-25. Score: 2 | Accepted answer

Keep in mind objects in PHP are always called by reference not by value thus your anchors query will always return blocks from the type subhead with the field value anchor =1 while your matrix query anchorLinks will return blocks of type copy with the field value anchor = 1 and anchorLink = 1
I'm not sure why you don't fetch the results in one single query as it would be faster and easier in my opinion but you want to create 2 queries you need to either reset the previously set property to null or use the clone  function 
{% set anchors2 = clone(entry.pageBuilder).type('copy').anchorLink('1').all() %}
{% set anchors1 = clone(entry.pageBuilder).type('subhead').anchor('1').all() %}

> **Comments:**
> **username_0**: Thanks! I somehow missed the changes in Craft 3 that make clone necessary. I would actually prefer to get the results in a single query, but I didn't think it was possible with the two different light switch values. Everything I tried gave me no results.  

---

