<p>Got some help and finally got this working. Here's the final code for my future self and anyone else that may need this: </p>

<pre><code>{% set thisService = entry.slug %}
{% set shufflePremium = shuffle(craft.users.group('premium').search('counselingServices:' ~thisService).ids()) %}
{% set shuffleBasic = shuffle(craft.users.group('listed').search('counselingServices:' ~thisService).ids()) %}
{% paginate craft.users().id(shufflePremium | merge(shuffleBasic)).limit(16).fixedOrder(true) as pageInfo, members %}

{% for user in members %}
  (...)
{% endfor %}
</code></pre>

CC BY-SA 4.0
2020-04-15T19:50:05.953Z
2020-04-15T19:50:05.953Z
A Herrera
<p>If you can live with ordering the member by for example the user UID then you could do something like this:</p>

<pre><code>{% set thisService = entry.slug %}

{% set users = craft.users().group(['premium', 'member']).search('counselingServices:'~thisService).orderBy('group desc, UID asc').limit(20) %}
{% paginate users as pageInfo, members %}

{% for member in members %}
    (...)
{% endfor %}
</code></pre>

<p>I'm not 100% on the syntax for ordering by user group, and I dont have a site with multiple groups to check atm, but its something like this.</p>

<p>With this the results would start with members / premium members together, but also ordered by UID, its not totally random, but close enough? UID contains both numbers and letters, so there is no 'incremental' value when ordering like this i think..</p>

CC BY-SA 4.0
2020-04-14T16:42:39.170Z
This ends up giving me a DB error on the ordering: https://d.pr/i/cRWvPc :/
A Herrera
CC BY-SA 4.0
2020-04-14T18:23:29.660Z
Have you tried ordering by groupId
Syversen
CC BY-SA 4.0
2020-04-15T01:30:39.503Z
Yep, I did. Same result.
A Herrera
CC BY-SA 4.0
2020-04-13T23:02:58.863Z
2020-04-13T23:02:58.863Z
Syversen
<p>I'm trying to get a list of users from two different user groups. One user group (premium) must always show first. Whenever the page is loaded, however, the listing of users should show randomly (while keeping premium users at the top of the list).</p>

<p>Also, the listing needs to filter on a particular service which is determined by the entry slug (in the user profiles, that field is set to an Entries fieldtype).</p>

<p>I've been able to get this like so:</p>

<pre><code>{% set thisService = entry.slug %}

{% set premiumMembers = craft.users().group('premium').search('counselingServices:'~thisService) %}
{% set shuffledPremium = shuffle(premiumMembers) %}
{% set basicMembers = craft.users().group('listed').search('counselingServices:'~thisService) %}
{% set shuffledBasic = shuffle(basicMembers) %}

{% set listedMembers = shuffledPremium|merge(shuffledBasic) %}
</code></pre>

<p>That all works great, but that of course is an array. I need to paginate the results which isn't possible with an array.</p>

<p>How can I paginate this? Or is there a better way to get the same results in an Element Criteria Model so that I can use the paginate tag?</p>

CC BY-SA 4.0
2020-04-13T23:26:39.107Z
maybe have a look at this? https://craftcms.stackexchange.com/questions/30343/paginating-merged-element-queries
erwinheiser
CC BY-SA 4.0
2020-04-14T16:22:58.350Z
Seems to work - kinda - but it kills memory at times. I end up with a PHP memory allocation error. :/
A Herrera
<pagination><array><merge>
Merge, shuffle, and paginate 
# Merge, shuffle, and paginate 

(Asked by: username_0 on 2020-04-13)

I'm trying to get a list of users from two different user groups. One user group (premium) must always show first. Whenever the page is loaded, however, the listing of users should show randomly (while keeping premium users at the top of the list).
Also, the listing needs to filter on a particular service which is determined by the entry slug (in the user profiles, that field is set to an Entries fieldtype).
I've been able to get this like so:
{% set thisService = entry.slug %}

{% set premiumMembers = craft.users().group('premium').search('counselingServices:'~thisService) %}
{% set shuffledPremium = shuffle(premiumMembers) %}
{% set basicMembers = craft.users().group('listed').search('counselingServices:'~thisService) %}
{% set shuffledBasic = shuffle(basicMembers) %}

{% set listedMembers = shuffledPremium|merge(shuffledBasic) %}

That all works great, but that of course is an array. I need to paginate the results which isn't possible with an array.
How can I paginate this? Or is there a better way to get the same results in an Element Criteria Model so that I can use the paginate tag?

**Tags:** pagination, array, merge

> **Comments:**
> **username_1**: maybe have a look at this? https://craftcms.stackexchange.com/questions/30343/paginating-merged-element-queries  
> **username_0**: Seems to work - kinda - but it kills memory at times. I end up with a PHP memory allocation error. :/  

---

## 2 answers

### Answer by username_0 on 2020-04-15. Score: 2

Got some help and finally got this working. Here's the final code for my future self and anyone else that may need this: 
{% set thisService = entry.slug %}
{% set shufflePremium = shuffle(craft.users.group('premium').search('counselingServices:' ~thisService).ids()) %}
{% set shuffleBasic = shuffle(craft.users.group('listed').search('counselingServices:' ~thisService).ids()) %}
{% paginate craft.users().id(shufflePremium | merge(shuffleBasic)).limit(16).fixedOrder(true) as pageInfo, members %}

{% for user in members %}
  (...)
{% endfor %}


---

### Answer by username_2 on 2020-04-13. Score: 1

If you can live with ordering the member by for example the user UID then you could do something like this:
{% set thisService = entry.slug %}

{% set users = craft.users().group(['premium', 'member']).search('counselingServices:'~thisService).orderBy('group desc, UID asc').limit(20) %}
{% paginate users as pageInfo, members %}

{% for member in members %}
    (...)
{% endfor %}

I'm not 100% on the syntax for ordering by user group, and I dont have a site with multiple groups to check atm, but its something like this.
With this the results would start with members / premium members together, but also ordered by UID, its not totally random, but close enough? UID contains both numbers and letters, so there is no 'incremental' value when ordering like this i think..

> **Comments:**
> **username_0**: This ends up giving me a DB error on the ordering: https://d.pr/i/cRWvPc :/  
> **username_2**: Have you tried ordering by groupId  
> **username_0**: Yep, I did. Same result.  

---

