<p>I've realised (I think) that I can just append <code>'and'</code> to <code>relArr</code> and then <a href="https://twig.symfony.com/doc/2.x/filters/reverse.html" rel="nofollow noreferrer">reverse</a> it.</p>
<pre><code>{% set relArr = relArr|length &gt; 1 ? relArr|merge(['and'])|reverse : relArr %}
</code></pre>
<p>Gonna try that.</p>

CC BY-SA 4.0
2022-09-27T01:23:21.250Z
2022-09-27T01:23:21.250Z
Jonathan Schofield
<p>I have 3 Matrix fields which define optional parameters for building a query:</p>
<ol>
<li>A related <code>Entries</code> field</li>
<li>A <code>Categories</code> field</li>
<li>A <code>Tags</code> field</li>
</ol>
<p>Any combination of them could be used, including none or all.</p>
<p>So I need to build a <code>relTo</code> array to pass into an entries query.</p>
<p>Given that the array might have a length of 1 or greater, it may or may not need an <code>'and'</code> element.</p>
<p><strong>Does that <code>'and'</code> have to be first?</strong></p>
<p>I'm assuming it does, so I'm doing something like this:</p>
<pre><code>{# Assume the field handles here exist as **block** fields and **entry** fields #}
{% set cats = block.relCats.all() ?? null %}
{% set rels = block.relEntries.all() ?? null %}
{% set tags = block.relTags.all() ?? null %}

{% set relArr = cats ? [{
    targetElement: cats, 
    field: 'relCats'
}] : [] %}

{% set relArr = rels ? relArr|merge([{
    targetElement: rels, 
    field: 'relEntries'
}]) : relArr %}

{% set relArr = tags ? relArr|merge([{
    targetElement: tags, 
    field: 'relTags'
}]) : relArr %}
</code></pre>
<p>So now I have an array which could be empty or have 1, 2 or 3 elements, and which <em>may</em> therefore require an <code>'and'</code>, so I’m assuming I need to do something like this:</p>
<pre><code>{# Build the relTo array #}
{% if relArr|length &gt; 1 %}
    {% set relTo = ['and'] %}

    {% for el in relArr %}
        {% set relTo = relTo|merge([ el ]) %}
    {% endfor %}
{% else %}
    {% set relTo = relArr %}
{% endif %}

{% if relTo|length %}
    {% set entries = craft.entries.section(handle)
        .relatedTo(relTo).limit(6).all() ?? null
    %}
{% else %}
    {% set entries = craft.entries.section(handle)
        .limit(6).all() ?? null
    %}
{% endif %}
</code></pre>
<p>Is this right, or <strong>is there a more elegant way to build the <code>relTo</code> array?</strong></p>
<p>I'm also now wondering if I need the <code>{% if relTo|length %}</code> conditional or if it's fine to pass an empty array into <code>relatedTo</code>, because it will just get ignored. (I guess I can test that.)</p>

<relatedto><array><elementquery><merge>
How best to merge an array into an array for a relatedTo query parameter?
# How best to merge an array into an array for a relatedTo query parameter?

(Asked by: username_0 on 2022-09-22)

I have 3 Matrix fields which define optional parameters for building a query:

A related Entries field
A Categories field
A Tags field

Any combination of them could be used, including none or all.
So I need to build a relTo array to pass into an entries query.
Given that the array might have a length of 1 or greater, it may or may not need an 'and' element.
Does that 'and' have to be first?
I'm assuming it does, so I'm doing something like this:
{# Assume the field handles here exist as **block** fields and **entry** fields #}
{% set cats = block.relCats.all() ?? null %}
{% set rels = block.relEntries.all() ?? null %}
{% set tags = block.relTags.all() ?? null %}

{% set relArr = cats ? [{
    targetElement: cats, 
    field: 'relCats'
}] : [] %}

{% set relArr = rels ? relArr|merge([{
    targetElement: rels, 
    field: 'relEntries'
}]) : relArr %}

{% set relArr = tags ? relArr|merge([{
    targetElement: tags, 
    field: 'relTags'
}]) : relArr %}

So now I have an array which could be empty or have 1, 2 or 3 elements, and which may therefore require an 'and', so I’m assuming I need to do something like this:
{# Build the relTo array #}
{% if relArr|length > 1 %}
    {% set relTo = ['and'] %}

    {% for el in relArr %}
        {% set relTo = relTo|merge([ el ]) %}
    {% endfor %}
{% else %}
    {% set relTo = relArr %}
{% endif %}

{% if relTo|length %}
    {% set entries = craft.entries.section(handle)
        .relatedTo(relTo).limit(6).all() ?? null
    %}
{% else %}
    {% set entries = craft.entries.section(handle)
        .limit(6).all() ?? null
    %}
{% endif %}

Is this right, or is there a more elegant way to build the relTo array?
I'm also now wondering if I need the {% if relTo|length %} conditional or if it's fine to pass an empty array into relatedTo, because it will just get ignored. (I guess I can test that.)

**Tags:** relatedto, array, elementquery, merge

---

## 1 answers

### Answer by username_0 on 2022-09-27. Score: 1

I've realised (I think) that I can just append 'and' to relArr and then reverse it.
{% set relArr = relArr|length > 1 ? relArr|merge(['and'])|reverse : relArr %}

Gonna try that.


---

