0

Let's say I have a tag group with handle genre.

I can make an entry contain information about this tag by creating a field of type tag that uses this group.

So I would have a field with handle genreTag added to the entry with handle movies.

(Note: I don't see the point of having to create an intermediary field, instead of just adding the tag selector to the entry. Why would I have multiple fields for the same tag group?)

Now I need to query entries that have a genre of adventure, for example.

I read this question, this question, the docs, and still don't get it:

As far as I understand, it would be done as follows:

{% set tag = craft.tags.genre('adventure').one() %}
{% if tag %}
    {% set entries = craft.entries.relatedTo(tag) %}
{% endif %}
{% dd entries %}

enter image description here

And doing it by the field handle instead of the tag handle:

{% set tag = craft.tags.genreTag('adventure').one() %}
{% if tag %}
    {% set entries = craft.entries.relatedTo(tag) %}
{% endif %}
{% dd entries %}

enter image description here

How should I approach this?

Thanks!

1 Answer 1

1

You need to fetch the actual tag before passing it into relatedTo(). The data you have to work with in your example ("adventure") is a tag slug, so query for that using .slug():

{% set tag = craft.tags.group('genre').slug('adventure').one() %}
{% if tag %}
    {% set entries = craft.entries.relatedTo(tag).all() %}
{% endif %}
{% dd entries %}

(...and don't forget to also execute your entries query with .all()).

I'm afraid I don't quite understand your other question about creating an "intermediary field"...? All you need to do is 1) create a tag group and 2) create a field that allows entries to select tags, optionally filtered by that group if you have multiple groups.

0

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.