<p><strong>Short version</strong><br>
change the title into an id and it will work</p>
<pre><code>{% set entries = craft.entries.section('journalArticles').relatedTo(
    'and', 
    { targetElement: &gt;&gt;idOfYourElement&lt;&lt; }, 
    { targetElement: &gt;&gt;idOfYourElement&lt;&lt; }) 
%}
</code></pre>
<p><strong>Long version</strong></p>
<p>It looks a little bit nasty (I never do these kind of things in twig because I don't like the syntax and prefer PHP for many reasons) but here you can see a working example for a twig search form</p>
<p>first your html: it could be like this</p>
<pre><code>&lt;form&gt;
    {# loop all your tags and display a checkbox for each one #}
    {# Important: insert the id of your tag as value not the title #}
    {% for tag in craft.tags.group('default') %}
        &lt;input type=&quot;checkbox&quot; name=&quot;selectedTags[]&quot; value=&quot;{{ tag.id }}&quot;&gt;{{ tag.title }}&lt;br&gt;
    {% endfor %}
    &lt;br&gt;
    &lt;input type=&quot;submit&quot; value=&quot;submit&quot; name=&quot;submit&quot;&gt;
&lt;/form&gt;
</code></pre>
<p>And your twig</p>
<pre><code>{% set isSubmitted = craft.request.getParam('submit') %}
{% if isSubmitted %}
    {# get all selected input ids #}
    {% set allRelatedTags = craft.request.getParam('selectedTags') %}
    
    {# proceed only if the user selected at least one tag #}
    {% if allRelatedTags %}
        {# build your related to array #}
        {% set relations = ['and'] %}
    
        {# inlcude an additional 'targetElement' for each selected tag #}
        {% for tag in allRelatedTags %}
            {% set relations =  relations|merge([{targetElement: tag}])%}
        {% endfor %}
        
        {# search for entries with the related parameters #}
        {% for entry in craft.entries.relatedTo(relations).find() %}
            {{ entry.title }}&lt;br&gt;
        {% endfor %}
    {% endif %}
{% endif %}
</code></pre>
<p>The reason why your code did not work is quite simple as you can see in the <a href="https://craftcms.com/docs/relations#the-relatedTo-param" rel="nofollow noreferrer">relatedTo documentation</a></p>
<blockquote>
<p>In its simplest form, you can pass in one of these things to it:</p>
<p>An AssetFileModel, CategoryModel, EntryModel, UserModel, or TagModel object</p>
<p>An element’s ID</p>
<p>An array of element objects and/or IDs</p>
</blockquote>
<p>you inserted the not unique title which could be everything. So all you have to do is to insert the id of your element instead of the title... that's it</p>

CC BY-SA 3.0
2018-02-13T12:20:37.287Z
Hopping on the laptop in a few to check this out. I would have rathered have done it in php like you said, but someone wrote this before me, and it would take a long time to refactor everything
Dan Zuzevich
CC BY-SA 3.0
2018-02-13T12:37:59.800Z
So the "id of the element" is what confuses me from the docs. What are you referring to when you say "the element"? Are you referring to the tag? If you are, then before I execute the short version of your solution, do I need to find the id of the tags, 'SCC' and 'HVT'?
Dan Zuzevich
CC BY-SA 3.0
2018-02-13T12:38:48.783Z
The search page has a good amount of logic, and I wish it would have been completed via a custom plugin.
Dan Zuzevich
CC BY-SA 3.0
2018-02-13T12:58:31.410Z
Do you have a little bit time? I would like to talk in chat with you https://chat.stackexchange.com/rooms/70444/plugin-developement I think you need to learn Craft wordings and typologies It won't require more than a few minutes
Robin Schambach
CC BY-SA 3.0
2018-02-13T08:57:59.763Z
2018-02-13T09:04:28.413Z
2020-06-17T08:56:27.903Z
Robin Schambach
<p>I have a search page where a user can check about 20 different checkboxes, where these checkboxes all relate to tags I have set up inside of Craft. I would like to perform the following search query:</p>

<p>For all of the tags selected, I would like to return a list of entries that have ALL of these tags. For example:</p>

<p><em>The user clicks the checkboxes, "SCC" and "HVT"</em></p>

<p>A query should be performed that returns only the entries that have the tags, "SCC" AND "HVT". I have been reading Craft docs for the past 3 hours, and am not grasping this concept at all.</p>

<p>I know this is hardcoded, but I was just experimenting, and it does not work at all:</p>

<pre><code>{% set entries = craft.entries.section('journalArticles').relatedTo('and', { targetElement: 'SCC' }, { targetElement: 'HPV' }) %}
</code></pre>

<p>I do not want to use a plugin if I don't have to.</p>

CC BY-SA 3.0
2018-02-12T23:27:15.950Z
I'm going to sleep right now, I'll update a proper answer tomorrow, I just wanted to give you a little hint to guide you to the correct direction: your targetElement must be an object (entry, asset, user, category) or the ID of an object. So you'll have to include the ID of `SCC`. Just a question. What do you mean with tags? Do you really mean tags (like real Craft tags) or just related categories with the corresponding title?
Robin Schambach
CC BY-SA 3.0
2018-02-12T23:29:28.227Z
Sweet. I appreciate it. I will attempt to figure it out,  but my workdays over now. Gotta eat =)
Dan Zuzevich
CC BY-SA 3.0
2018-02-13T12:46:45.270Z
I was referring to real tags
Dan Zuzevich
<tags>
Get Entries That Match ALL of the tags specified
# Get Entries That Match ALL of the tags specified

(Asked by: username_0 on 2018-02-12)

I have a search page where a user can check about 20 different checkboxes, where these checkboxes all relate to tags I have set up inside of Craft. I would like to perform the following search query:
For all of the tags selected, I would like to return a list of entries that have ALL of these tags. For example:
The user clicks the checkboxes, "SCC" and "HVT"
A query should be performed that returns only the entries that have the tags, "SCC" AND "HVT". I have been reading Craft docs for the past 3 hours, and am not grasping this concept at all.
I know this is hardcoded, but I was just experimenting, and it does not work at all:
{% set entries = craft.entries.section('journalArticles').relatedTo('and', { targetElement: 'SCC' }, { targetElement: 'HPV' }) %}

I do not want to use a plugin if I don't have to.

**Tags:** tags
> **Comments:**
> **username_1**: I'm going to sleep right now, I'll update a proper answer tomorrow, I just wanted to give you a little hint to guide you to the correct direction: your targetElement must be an object (entry, asset, user, category) or the ID of an object. So you'll have to include the ID of `SCC`. Just a question. What do you mean with tags? Do you really mean tags (like real Craft tags) or just related categories with the corresponding title?  
> **username_0**: Sweet. I appreciate it. I will attempt to figure it out,  but my workdays over now. Gotta eat =)  
> **username_0**: I was referring to real tags  

---

## 1 answers

### Answer by username_1 on 2018-02-13. Score: 2 | Accepted answer

Short version
change the title into an id and it will work
{% set entries = craft.entries.section('journalArticles').relatedTo(
    'and', 
    { targetElement: >>idOfYourElement<< }, 
    { targetElement: >>idOfYourElement<< }) 
%}

Long version
It looks a little bit nasty (I never do these kind of things in twig because I don't like the syntax and prefer PHP for many reasons) but here you can see a working example for a twig search form
first your html: it could be like this

    {# loop all your tags and display a checkbox for each one #}
    {# Important: insert the id of your tag as value not the title #}
    {% for tag in craft.tags.group('default') %}
        {{ tag.title }}
    {% endfor %}
    
    


And your twig
{% set isSubmitted = craft.request.getParam('submit') %}
{% if isSubmitted %}
    {# get all selected input ids #}
    {% set allRelatedTags = craft.request.getParam('selectedTags') %}
    
    {# proceed only if the user selected at least one tag #}
    {% if allRelatedTags %}
        {# build your related to array #}
        {% set relations = ['and'] %}
    
        {# inlcude an additional 'targetElement' for each selected tag #}
        {% for tag in allRelatedTags %}
            {% set relations =  relations|merge([{targetElement: tag}])%}
        {% endfor %}
        
        {# search for entries with the related parameters #}
        {% for entry in craft.entries.relatedTo(relations).find() %}
            {{ entry.title }}
        {% endfor %}
    {% endif %}
{% endif %}

The reason why your code did not work is quite simple as you can see in the relatedTo documentation

In its simplest form, you can pass in one of these things to it:
An AssetFileModel, CategoryModel, EntryModel, UserModel, or TagModel object
An element’s ID
An array of element objects and/or IDs

you inserted the not unique title which could be everything. So all you have to do is to insert the id of your element instead of the title... that's it

> **Comments:**
> **username_0**: Hopping on the laptop in a few to check this out. I would have rathered have done it in php like you said, but someone wrote this before me, and it would take a long time to refactor everything  
> **username_0**: So the "id of the element" is what confuses me from the docs. What are you referring to when you say "the element"? Are you referring to the tag? If you are, then before I execute the short version of your solution, do I need to find the id of the tags, 'SCC' and 'HVT'?  
> **username_0**: The search page has a good amount of logic, and I wish it would have been completed via a custom plugin.  
> **username_1**: Do you have a little bit time? I would like to talk in chat with you https://chat.stackexchange.com/rooms/70444/plugin-developement I think you need to learn Craft wordings and typologies It won't require more than a few minutes  

---

