2

I am trying to output a list of entries from a channel section that DO NOT have a certain tag.

I am able to output the entries that have the tag assigned by setting the tag as a variable and using relatedTo to filter the entries out. For example:

{% set tag = craft.tags.search('All-Stars').first() %}

{% if tag %}
{% set relatedEntries = craft.entries.section('news').relatedTo(tag) %}
{% endif %}

What is the best approach to output entries without the tag? I've tried the following:

{% set tag = craft.tags.search('All-Stars').first() %}
{% set allEntries = craft.entries.section('news') %}
{% set taggedEntries = craft.entries.section('news').relatedTo(tag) %}
{% set untaggedEntries = allEntries|without(taggedEntries) %}
{% for feedItems in untaggedEntries.limit(5).offset(1) %}

This however doesn't work and throws out some errors, the first being:

array to string conversion

craft/app/vendor/twig/twig/lib/Twig/Template.php(525)

$message = sprintf('Impossible to invoke a method ("%s") on a %s variable ("%s")', $item, gettype($object), $object);

I tried many other ideas like .relatedTo('not' ~ tag) and |without with no avail.

Is it something obvious I am missing or more advance than that?

Thanks in advance

3

You could try something like following reducing the tagged entries into IDs that you then filter against (see this thread):

{% set tag = craft.tags.search('All-Stars').first() %}
{% set allEntries = craft.entries.section('news') %}
{% set taggedEntries = craft.entries.section('news').relatedTo(tag).ids %}
{% set omitIds = 'and, not ' ~ taggedEntries|join(', not ') %}
{% set untaggedEntries = craft.entries.section('news').id(omitIds) %}
| improve this answer | |
  • exactly what I was looking for! – Tad Ward Mar 14 '17 at 17:09

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.