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?

Paul
  • 6,338
  • 12
  • 26

1 Answers1

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) %}
Brandon Kelly
  • 34,307
  • 2
  • 71
  • 137