1

I have searched and tried a few different ways but none have worked so not sure what is wrong.

Example say I have a few categories case studies, featured, articles, videos...

I have tried this way

{% set category = craft.categories.group('resource').relatedTo(
    'and',
    { targetElement: featured },
    { targetElement: case studies }
) %}
{% set entries = craft.entries.relatedTo(category).limit(3).find() %}

I have tried this way

{% set category = craft.categories({
    group: 'resource',
    relatedTo: [
        'and',
        { targetElement: featured },
        { targetElement: case-studies }
    ]
}) %}
{% set entries = craft.entries.relatedTo(category).limit(3).find() %}

I am wanting to find entries that use two categories together example case studies, and featured category.

1 Answer 1

6

The relatedTo parameter's targetElement property has to be an ElementCriteriaModel or a single category model:

{% set featured = craft.categories.group('myCatGroup').slug('featured').first() %}
{% set caseStudies = craft.categories.group('myCatGroup').slug('case-studies') %}

{% set entries = craft.users({
    group: 'resource',
    relatedTo: [
        'and',
        { targetElement: featured },
        { targetElement: caseStudies }
    ]
}) %}
2
  • Why are we setting .first() for the featured category? This does work though just curious why the .first().
    – Bowenac
    May 20, 2015 at 13:30
  • It was just an example for a single category model instead of an ElementCriteriaModel.
    – carlcs
    May 20, 2015 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.