is it possible to grab all the entries in a section that arent related to a category?
So something like
craft.entries.section('news').relatedTo('not',category)
Doesnt seem to work but is there some other syntax?
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It only takes a minute to sign up.
Sign up to join this communityIf you want to do it in a single query, Marion's answer is probably the best.
Otherwise, you could do something like this:
{% set idsHavingCategory = craft.entries.section('news').relatedTo(category).limit(null).ids() %}
{% set omitIds = 'and, not ' ~ idsHavingCategory|join(', not ') %}
{% set entries = craft.entries.section('news').id(omitIds) %}
Inspired by this article, which shows how to (1) grab ids, and (2) query against them:
http://webstoemp.com/blog/manipulating-craft-elementcriteriamodel-with-twig/
Also references related solutions found in these answers:
I got this to work with search:
{% set uncategorizedEntries
= craft.entries.section('news').search('-category:*') %}
Here category
is the category field. You are searching everything in news
which does not have a category
field that matches *
.
Another approach -- that might work for you -- is to filter on the output, rather than the query. For instance, using this query:
{% set entries = craft.entries.section('news') %}
Output using this loop:
{% for entry in entries if entry.category is empty %}
...
{% endfor %}
YMMV
Not sure if this will help much, but I did something similar with:
{% set allNews = craft.entries.section('news') %)
Then:
{% for entry in allNews.id("not #{category.id}")
... {% endfor %}
Hope that helps in some way.