5

I tried to get all tags that are related to an section. Everything I tried failed. Is there a way to get all assigned tags for a particular element type by using element criteria model. In this case travelImages? I want to avoid to display not related tags.

'elementType' => 'Tag',
'criteria' => ['relatedto' => 'travelImages']
3

The relatedTo parameter accepts either a single element ID or model, or an array of element IDs or models. It doesn't accept asset source handles, section handles, or a particular element type constant.

This means that in order to pull all tags related to a specific element type, you'll need to give the relatedTo parameter an array of all IDs for that particular element type.

You're not specific about what travelImages are, but assuming it's an Asset source, something like this should work:

'elementType' => ElementType::Tag,
'criteria' => [
    'relatedTo' => craft()->elements->getCriteria(ElementType::Asset, [
        'limit' => null,
    ])->ids(),
    'limit' => null,
],

Note that if travelImages isn't an Asset source, you'll need to change ElementType::Asset to the appropriate element type (e.g. ElementType::Entry, ElementType::Category etc).

| improve this answer | |
  • Doesn't work. Got an exception: CException Craft\WebApp and its behaviors do not have a method or closure named "entries". – Siggi Foster Oct 1 '16 at 19:24
  • @SiggiFoster Sorry about that – I was thinking in Twig syntax :) See updated answer above. – Mats Mikkel Rummelhoff Oct 2 '16 at 7:07
  • 1
    Works perfect. You were right. I forgot to mention what type of element travelImages are. It's an entry element type. Changed it to ElementType::Entry and problem solved. Thank you very much. – Siggi Foster Oct 4 '16 at 18:46

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.