I want to get a list of entries in a template that have one specific tag 'weddings' and exclude entries that do not have this tag.

{% set tag = craft.tags.search('weddings').first() %}

{% if tag %}          
  {% set entries = craft.entries.relatedTo(tag) %}
  {% for entry in craft.entries.section('blog').limit(5) %}
    {# get stuff #}
  {% endfor %}
{% endif %}

But this gets me all blog entries - even those without the Tag weddings. What am I missing?


Luke's answer is correct. Your issue is that you have two tag groups defined (default and blogTags) and you happen to have a weddings tag defined for both groups.

The Tag field you have in your blog section has it source limited to the blogTags tag group, so you need to be explicit about which tags you want to pull from because of the duplicate tags.

{% set tag = craft.tags.group('blogTags').slug('weddings').first() %}
{% set entries = craft.entries.relatedTo(tag) %}
  • thanks Brad and Luke - so darn obvious in the end. Much appreciated support
    – joomkit
    Jul 1 '16 at 15:49

You were very close! You are looping through craft.entries.section('blog') which doesn't use your tag query. You simply need to loop through the results of your query which you've set in the entries variable.

You may want to grab your weddings tag using the tag's id or slug as .search will find any tag with the string 'weddings' in it.

It might also be a good idea to use the conditional to check entries is returning something first, before outputting it.

{% set tag = craft.tags.slug('weddings').first() %}
{% set entries = craft.entries.relatedTo(tag) %}

{% if entries|length %}
  {% for entry in entries.limit(5) %}
    {# get stuff #}
  {% endfor %}
{% endif %}
  • Thanks Luke - thats not working for me - i get zero results.....
    – joomkit
    Jun 27 '16 at 16:11
  • Try using .title instead of .slug and change 'weddings` to the tags Title. Jun 27 '16 at 16:16
  • Mmm something is not right here. The title is 'weddings' lowercase. I noticed that some tags were 'wedding flowers' and read something about spaces causing issues in tags..so deleted tags.and re added...but still getting nothing
    – joomkit
    Jun 28 '16 at 8:14
  • .title and .slug will only return absolute matches. If you want to return any tags that include 'weddings' then you'll need to use .search just you were to begin with. Jun 28 '16 at 8:29
  • 1
    Still i get nothing output....Mmm gonna call Brandon direct!
    – joomkit
    Jun 28 '16 at 10:44

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.