5

In my template, I want to:

  • Get all the entries in the section 'uploads';
  • Get all the assets that are related to one of the upload entries, and have a relation to all the tags I provide.

So, that would be:

{% set company = craft.entries.section('company').id(companyId).first() %}
{% set uploads = craft.entries.section('uploads').relatedTo(company).limit(null) %}

{% set relatedToUploads = ['or'] %}
{% for upload in uploads %}
    {% set relatedToUploads = relatedToUploads | merge([upload.id]) %}
{% endfor %}

{% set relatedToTags = ['and'] %}
{% for tag in tags %}
    {% set relatedToTags = relatedToTags | merge([tag.id]) %}
{% endfor %}

But, how would I combine those two relatedTo-arrays to one, usable in the ElementCriteriaModel for the assets?

  • Where is carlsc and his ElementCriteriaModel foo when you need him?! You might check out this post or this post for some hints on combining and/or and merging elements. It may help. – Douglas McDonald Nov 4 '14 at 23:49
4

It’s not currently possible to specify both of those conditions at once with the relatedTo param, so you would have to do it with two separate queries.

First, get all of the asset IDs that are related to the upload entries:

{% set uploadAssetIds = craft.assets.relatedTo(uploads).ids() %}

(Note that the relatedToUploads variable is not actually necessary here.)

Then you would get all of the assets whose ID falls within one of the uploadAssetIds, and who are related to all of the tag IDs.

{% set relatedToTags = ['and'] %}
...

{% set assets = craft.assets.id(uploadAssetIds).relatedTo(relatedToTags) %}

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.