<p>Since you already have a working solution, I'll just leave a couple of suggestions to improve your code here. If you only need the tag for the <code>relatedTo</code> attribute, there's no need to load the entire element – instead, execute the query with <code>ids()</code>, this way you only get the tag ID(s) and can pass those to the relatedTo function:</p>
<pre class="lang-twig prettyprint-override"><code>{% set tagIds = craft.tags.slug(tagfilter).ids() %}
</code></pre>
<p>This is better for performance reasons and has another nice benefit: If you ever change your tag dropdown to a multi-select or to checkboxes (allowing the user to select multiple categories to filter by), this will just keep on working – by default, this will find all entries that are related to any of the selected tags, though you can change that to only match entries that are releated to <em>all</em> of them.</p>
<p>Now if you have multiple dropdowns for different tag fields, you only need to adjust this slightly to get all selected tag IDs:</p>
<pre class="lang-twig prettyprint-override"><code>{% set selectedTagSlugs = [
    craft.app.request.getParam('locationNuts'),
    craft.app.request.getParam('fundingProgramme'),
    craft.app.request.getParam('localAuthority'),
]|filter(v =&gt; v is not empty) %}
{% set tagIds = craft.tags.slug(selectedTagSlugs).ids() %}
</code></pre>
<p>Side note: If it's likely that different tag groups have tags with the same slug, you'll want to use separate queries for those and <code>merge</code> them together to get one array of IDs for the entries query.</p>
<p>Finally, you can further narrow down your entries query to only the relevant tag fields, and only to relations where the tag is the <code>target</code>:</p>
<pre class="lang-twig prettyprint-override"><code>{% set sentries = craft.entries({
    search: query,
    section: ['projects','projectStories'],
    relatedTo: {
        targetElement: tagIds,
        field: ['locationNuts', 'fundingProgramme', 'localAuthority'],
    },
    order: 'score'
}) %}
</code></pre>

CC BY-SA 4.0
2022-02-14T11:07:31.383Z
2022-02-14T11:07:31.383Z
MoritzLost
<p>My search form has a drop down of tags from different tag groups.</p>
<p>My current search code for the input query is</p>
<pre><code>{% set sentries = craft.entries({
                    search: query,
                    section: ['projects','projectStories'],
                    order: 'score'
                }) %}
</code></pre>
<p>How do I take the single tag (drop down select value) and find entries from these sections in the search?</p>
<p>I have 4 tag groups and the select tag for search can be from any one them.
Eg my tag groups look like:</p>
<pre><code>       {% set searchLocationNuts = craft.tags().group('locationNuts')  %}
       {% set searchFundingP = craft.tags().group('fundingProgramme')  %}
       {% set searchLA = craft.tags().group('localAuthority')  %}
</code></pre>
<p>Im not sure if relatedTo is meant to be used with targetElement or if there is a needto grab all tag ids and merge them for search. And i am not sure if the tag search should be tag name or tag slug.</p>
<p>Pointers welcome:)</p>
<p>** Solution **</p>
<pre><code>{% set tagfilter = craft.app.request.getParam('tagfilter') %}
                {% set tag = craft.tags.slug(tagfilter).one() %}
                {% set query = craft.app.request.getParam('q') %}
                
                {% set sentries = craft.entries({
                    search: query,
                    section: ['projects','projectStories'],
                    relatedTo: tag,
                    order: 'score'
                }) %}
</code></pre>

<search><tags>
How do I search entries by multiple tags that are in different tag groups?
# How do I search entries by multiple tags that are in different tag groups?

(Asked by: username_0 on 2022-02-11)

My search form has a drop down of tags from different tag groups.
My current search code for the input query is
{% set sentries = craft.entries({
                    search: query,
                    section: ['projects','projectStories'],
                    order: 'score'
                }) %}

How do I take the single tag (drop down select value) and find entries from these sections in the search?
I have 4 tag groups and the select tag for search can be from any one them.
Eg my tag groups look like:
       {% set searchLocationNuts = craft.tags().group('locationNuts')  %}
       {% set searchFundingP = craft.tags().group('fundingProgramme')  %}
       {% set searchLA = craft.tags().group('localAuthority')  %}

Im not sure if relatedTo is meant to be used with targetElement or if there is a needto grab all tag ids and merge them for search. And i am not sure if the tag search should be tag name or tag slug.
Pointers welcome:)
** Solution **
{% set tagfilter = craft.app.request.getParam('tagfilter') %}
                {% set tag = craft.tags.slug(tagfilter).one() %}
                {% set query = craft.app.request.getParam('q') %}
                
                {% set sentries = craft.entries({
                    search: query,
                    section: ['projects','projectStories'],
                    relatedTo: tag,
                    order: 'score'
                }) %}

**Tags:** search, tags


---

## 1 answers

### Answer by username_1 on 2022-02-14. Score: 1 | Accepted answer

Since you already have a working solution, I'll just leave a couple of suggestions to improve your code here. If you only need the tag for the relatedTo attribute, there's no need to load the entire element – instead, execute the query with ids(), this way you only get the tag ID(s) and can pass those to the relatedTo function:
{% set tagIds = craft.tags.slug(tagfilter).ids() %}

This is better for performance reasons and has another nice benefit: If you ever change your tag dropdown to a multi-select or to checkboxes (allowing the user to select multiple categories to filter by), this will just keep on working – by default, this will find all entries that are related to any of the selected tags, though you can change that to only match entries that are releated to all of them.
Now if you have multiple dropdowns for different tag fields, you only need to adjust this slightly to get all selected tag IDs:
{% set selectedTagSlugs = [
    craft.app.request.getParam('locationNuts'),
    craft.app.request.getParam('fundingProgramme'),
    craft.app.request.getParam('localAuthority'),
]|filter(v => v is not empty) %}
{% set tagIds = craft.tags.slug(selectedTagSlugs).ids() %}

Side note: If it's likely that different tag groups have tags with the same slug, you'll want to use separate queries for those and merge them together to get one array of IDs for the entries query.
Finally, you can further narrow down your entries query to only the relevant tag fields, and only to relations where the tag is the target:
{% set sentries = craft.entries({
    search: query,
    section: ['projects','projectStories'],
    relatedTo: {
        targetElement: tagIds,
        field: ['locationNuts', 'fundingProgramme', 'localAuthority'],
    },
    order: 'score'
}) %}


---

