1

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'
                }) %}

1 Answer 1

1

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'
}) %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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