2

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?

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 %}
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..

  • This ends up giving me a DB error on the ordering: d.pr/i/cRWvPc :/ – Angie Herrera Apr 14 '20 at 16:42
  • Have you tried ordering by groupId – Syversen Apr 14 '20 at 18:23
  • Yep, I did. Same result. – Angie Herrera Apr 15 '20 at 1:30

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.