<p>The <code>relatedTo</code> function accepts multiple different formats. If you want to search for Elements that are related to a list of categories and a list of tags you can do that like this</p>

<pre class="lang-PHP prettyprint-override"><code>relatedTo([
    'and',
    ['element' =&gt; [1, 2, 3]],
    ['element' =&gt; [4, 5, 6]]
]);
</code></pre>

<p>This will return you all Elements that are related to the first array and related to the second array. So you goal should be to create an array of category IDs and Tag IDs and pass them together in the <code>relatedTo</code></p>

<p>To get Elements that are related to all elements of one array and a single one from another array do the following</p>

<pre class="lang-PHP prettyprint-override"><code>relatedTo([
    'and',
    ['element' =&gt; [1, 2, 3]],
    ['element' =&gt; ['and', 4, 5, 6]]
]);
</code></pre>

CC BY-SA 4.0
2019-02-20T15:07:13.573Z
Thank you for this it has helped, although I haven't yet managed to succeed with the second example. I have in any case used something similar to the first example you give, but using 'or' for our tags array and then filtered the result to only show entries in the categories we need.
Martin
CC BY-SA 4.0
2019-02-20T15:19:47.577Z
The Code itself works, I tested and used it many times so without knowing your exact issue there is nothing I can really do. Furthermore it's a little bit difficult what you really want. What does not work exactly?
Robin Schambach
CC BY-SA 4.0
2019-02-20T16:22:52.783Z
Sorry I am sure your code does work. We are just not sure how to recreate your second example in our template 

This is what we have working at present:

{% set relatedToParam = ['or'] %}
{% for id in selectedTagIds %}
    {% set relatedToParam = relatedToParam|merge([{
        targetElement: id
    }]) %}
{% endfor %}

Using relatedTo(relatedToParam) we get entries that match ANY of the tags

Then within the results we only want to show matched tags from a particular category
Martin
CC BY-SA 4.0
2019-02-20T16:57:53.470Z
So you only want to match tags that are related with a particular category? By the way: creating such nested arrays in Twig is a real pain. It's easier with PHP
Robin Schambach
CC BY-SA 4.0
2019-02-18T17:44:10.763Z
2019-02-18T17:44:10.763Z
Robin Schambach
<p>This works for filtering by tag array </p>

<pre><code>{% set relatedToParam = ['or'] %}

{% for id in selectedTagIds %}
    {% set relatedToParam = relatedToParam|merge([{
        targetElement: id
    }]) %}
{% endfor %}

{% set entries = craft.entries.relatedTo(relatedToParam) %}
</code></pre>

<p>But how can we add another relatedTo param for category so the results only show the selected tag entries from a category?</p>

<p>For example to achieve this:</p>

<pre><code>...WHERE categoryid = 'myid' AND (tag1 LIKE 'green' OR tag2 LIKE 'red' OR tag3 LIKE 'yellow' etc)
</code></pre>

<filtering>
How to filter entries by tags array using ['or'] and by category not using or
# How to filter entries by tags array using ['or'] and by category not using or

(Asked by: username_0 on 2019-02-18)

This works for filtering by tag array 
{% set relatedToParam = ['or'] %}

{% for id in selectedTagIds %}
    {% set relatedToParam = relatedToParam|merge([{
        targetElement: id
    }]) %}
{% endfor %}

{% set entries = craft.entries.relatedTo(relatedToParam) %}

But how can we add another relatedTo param for category so the results only show the selected tag entries from a category?
For example to achieve this:
...WHERE categoryid = 'myid' AND (tag1 LIKE 'green' OR tag2 LIKE 'red' OR tag3 LIKE 'yellow' etc)

**Tags:** filtering


---

## 1 answers

### Answer by username_1 on 2019-02-18. Score: 2

The relatedTo function accepts multiple different formats. If you want to search for Elements that are related to a list of categories and a list of tags you can do that like this
relatedTo([
    'and',
    ['element' => [1, 2, 3]],
    ['element' => [4, 5, 6]]
]);

This will return you all Elements that are related to the first array and related to the second array. So you goal should be to create an array of category IDs and Tag IDs and pass them together in the relatedTo
To get Elements that are related to all elements of one array and a single one from another array do the following
relatedTo([
    'and',
    ['element' => [1, 2, 3]],
    ['element' => ['and', 4, 5, 6]]
]);

> **Comments:**
> **username_0**: Thank you for this it has helped, although I haven't yet managed to succeed with the second example. I have in any case used something similar to the first example you give, but using 'or' for our tags array and then filtered the result to only show entries in the categories we need.  
> **username_1**: The Code itself works, I tested and used it many times so without knowing your exact issue there is nothing I can really do. Furthermore it's a little bit difficult what you really want. What does not work exactly?  
> **username_0**: Sorry I am sure your code does work. We are just not sure how to recreate your second example in our template   This is what we have working at present:  {% set relatedToParam = ['or'] %} {% for id in selectedTagIds %}     {% set relatedToParam = relatedToParam|merge([{         targetElement: id     }]) %} {% endfor %}  Using relatedTo(relatedToParam) we get entries that match ANY of the tags  Then within the results we only want to show matched tags from a particular category  
> **username_1**: So you only want to match tags that are related with a particular category? By the way: creating such nested arrays in Twig is a real pain. It's easier with PHP  

---

