TransWikia.com

Get entries that aren't related to current user

Craft CMS Asked by Less and More Collective on November 29, 2020

I am trying to get all the entries that don’t related to the current user via a user field.

I have used the following code to fetch all the entries that do related to the current user

{% set entries = craft.entries.section('movementCoursesEntries').level(1).relatedTo({
   targetElement: currentUser,
   field: 'permittedUsers'
}).find() %} 

When I try to exclude these entries from the rest of the entries in the structure, I get nothing

{% set unrelated = craft.entries.section('movementCoursesEntries').level(1)|without(entries) %}

{% for entry in unrelated %}
    {{ entry.title }}
{% endfor %}

Any idea how to filter those entries properly?
Thank you!

One Answer

Try this:

{% set entries = craft.entries.section('movementCoursesEntries').level(1).relatedTo({
   targetElement: currentUser,
   field: 'permittedUsers'
}).ids() | join(', not ') %}

{% set unrelated = craft.entries.section('movementCoursesEntries').level(1).id('and, not '~entries).all() %}

{% for entry in unrelated %}
    {{ entry.title }}
{% endfor %}

Correct answer by Pavel Pavlovich on November 29, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP