1

I have a search page that filters with these params:

{% set results = craft.entries({
    section: 'properties',
    price: range,
    search: query.location,
    relatedTo: status,
    order: priceDesc
}) %}

The status variable needs to be set to a 'not' this category (or an array of all the other categories), but I can't seem to phrase my param correctly.

I've tried both the string and array method with category ID's, like this:

relatedTo: ['not', category.id]

And this:

relatedTo: ['or', category1.id, category2.id, ...]

Advice?

1

You need to exclude the ids in order to achieve your goal. So instead of relatedTo not you need a normal related to but the not statement in your id

I'm not on my computer currently so I'll edit the exact code in about 2h but it's the same like that one https://craftcms.stackexchange.com/a/22532/5557

id.('and, not ' ~ idsOfEntriesYouWantToExclude|join(', not ') Is the important part. In order to get them you just select all entries that are related to the thing you want to exclude

Edit
untested but this should do it

{% relatedEntries = craft.entries.relatedTo(status).ids() %}
{# just for debugging to test if the queryString is correct #}
{% set queryString = 'not ' ~ relatedEntries|join(', not ') %}
{% set results = craft.entries({
    id: 'and, ' ~ queryString,
    section: 'properties',
    price: range,
    search: query.location,
    order: priceDesc
}) %}
  • Thanks Robin! I had not thought of that approach. FYI for future reference, I finally got relatedTo: ['or', catId1, catId2, ...] to work. It looks like that parameter accepts 'or' and 'and', but not 'not'. My category list has 4 items and I was trying to exclude one, so I set my category ids to craft.categories.id('not' ~ omitCatId).ids(), then merged `['or]' to it and passed the array to relatedTo. – Joshua Oct 27 '17 at 15:16

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.