<p>From Twig, this seems like a decent solution.</p>

<p>Could possibly be streamlined a bit if you wanted to do it from PHP, but as long as you're not dealing with large amounts of images, it's probably not worth it.</p>

CC BY-SA 4.0
2021-04-27T17:31:14.947Z
Is it possible to modify the query so that the resulting image array order respects that of the original arrays? (Eg client logos in order from the first array, then agency logos in order from the second array)
James Greig
CC BY-SA 4.0
2019-02-13T23:57:17.727Z
2019-02-13T23:57:17.727Z
Brad Bell
<p>Assuming this is an entry page and you don't have other asset fields (though you could use <code>targetElement</code>), you could fetch assets <code>relatedTo</code> your current entry?</p>

CC BY-SA 4.0
2019-02-14T11:48:53.967Z
2019-02-14T11:48:53.967Z
Oli
<p>I've got this working but it seems terribly verbose to do something, that in my opinion should be easier.</p>

<p>Is there a better way to get the assets from 2 different fields, merge them and output them in a random order?</p>

<pre><code>{% set clientLogos = logos.clientLogos.all() %}
{% set agencyLogos = logos.agencyLogos.all() %}

{# set empty array #}
{% set logoIds = [] %}

{# add logos from clients #}
{% for logo in clientLogos %}
  {% set logoIds = logoIds|merge([logo.id]) %}
{% endfor %}

{#  add logos from agencies #}
{% for logo in agencyLogos %}
  {% set logoIds = logoIds|merge([logo.id]) %}
{% endfor %}

{# looped through merged logos #}
{% for logo in craft.assets().volume('uploads').kind('image').id(logoIds).orderBy('RAND()') %}
  &lt;img src="{{ logo.url(croppedlogo) }}" alt="{{ logo.title }}" class="m-4"/&gt;
{% endfor %}  
</code></pre>

<templating><assets><merge><assets-field>
Merging Assets Fields
# Merging Assets Fields

(Asked by: username_0 on 2019-02-13)

I've got this working but it seems terribly verbose to do something, that in my opinion should be easier.
Is there a better way to get the assets from 2 different fields, merge them and output them in a random order?
{% set clientLogos = logos.clientLogos.all() %}
{% set agencyLogos = logos.agencyLogos.all() %}

{# set empty array #}
{% set logoIds = [] %}

{# add logos from clients #}
{% for logo in clientLogos %}
  {% set logoIds = logoIds|merge([logo.id]) %}
{% endfor %}

{#  add logos from agencies #}
{% for logo in agencyLogos %}
  {% set logoIds = logoIds|merge([logo.id]) %}
{% endfor %}

{# looped through merged logos #}
{% for logo in craft.assets().volume('uploads').kind('image').id(logoIds).orderBy('RAND()') %}
  
{% endfor %}

**Tags:** templating, assets, merge, assets-field

---

## 2 answers

### Answer by username_1 on 2019-02-13. Score: 1 | Accepted answer

From Twig, this seems like a decent solution.

Could possibly be streamlined a bit if you wanted to do it from PHP, but as long as you're not dealing with large amounts of images, it's probably not worth it.

> **Comments:**
> **username_2**: Is it possible to modify the query so that the resulting image array order respects that of the original arrays? (Eg client logos in order from the first array, then agency logos in order from the second array)  

---

### Answer by username_3 on 2019-02-14. Score: 0

Assuming this is an entry page and you don't have other asset fields (though you could use `targetElement`), you could fetch assets `relatedTo` your current entry?


---

