1

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.

  • 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? – Robin Schambach Feb 12 '18 at 23:27
  • Sweet. I appreciate it. I will attempt to figure it out, but my workdays over now. Gotta eat =) – Dan Zuzevich Feb 12 '18 at 23:29
  • I was referring to real tags – Dan Zuzevich Feb 13 '18 at 12:46
2

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

<form>
    {# 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') %}
        <input type="checkbox" name="selectedTags[]" value="{{ tag.id }}">{{ tag.title }}<br>
    {% endfor %}
    <br>
    <input type="submit" value="submit" name="submit">
</form>

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 }}<br>
        {% 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

  • 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 Feb 13 '18 at 12:20
  • 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 Feb 13 '18 at 12:37
  • The search page has a good amount of logic, and I wish it would have been completed via a custom plugin. – Dan Zuzevich Feb 13 '18 at 12:38
  • Do you have a little bit time? I would like to talk in chat with you 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 Feb 13 '18 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.