2

I am trying to order a list of Users by the date of their last entry. order('postDate desc') works for entries of course, but I can't find a way to order the Users of those posts based on their latest post date. This is the code I am using now which orders the User by the date their account was created.

{# Grab all the Design Entries #}
{% set allDesigns = craft.entries.section('designs').limit(null).find() %}

{# Group by the authorId #}
{% set allDesignsByAuthorId = allDesigns|group('authorId') %}

{# Grab all of the users with the userIds in allDesignsByAuthorId #}
{% set designers = craft.users.group('designers').id(allDesignsByAuthorId|keys).order('dateCreated desc') %}

Thank you in advance for any help you can give!

2

I just noticed that you've posted an answer to your question already.

Your approach using the group filter is probably all you need, as the allDesigns array is sorted by postDate desc and by using the group filter you'll keep that sort order, your author IDs and the nested entries will both be sorted by the design post date.

To query the user models in the order of the IDs passed to the id param, you just need to add the fixedOrder param to your craft.users criteria model.

{# Grab all the Design Entries #}
{% set allDesigns = craft.entries.section('designs').limit(null).find() %}

{# Group by the authorId #}
{% set allDesignsByAuthorId = allDesigns|group('author.id') %}

{# Grab all of the users with the userIds in allDesignsByAuthorId #}
{% set designers = craft.users.group('designers').id(allDesignsByAuthorId|keys).fixedOrder(true) %}
| improve this answer | |
  • Thanks or these options @carlcs! I will add the fixedOrder to this. :) – Angela Jan 26 '16 at 2:28
  • I used the .fixedOrder(true) on the code above, and it is working on my local development site, but when I push to the live server, the page / section this is on seems to be stuck loading indefinitely with a blank screen. (Can't even view source code - it says "waiting...) Any ideas on what may be causing that? – Angela Jan 27 '16 at 2:51
  • I think I found the issue in the PHP Error logs. Unfortunately the query is over the memory limit - cl.ly/3R0s140t2a33 - I haven't run into this issue before so any tips you can give would be appreciated. :) Thank you! – Angela Jan 27 '16 at 3:01
1

You could do something like this: get all your designer users, loop them and for each user you try to get the latest design entry, if there exists one, merge it to a custom array which uses the post date of the entry as the key and the designer's user model as the value.

You can now sort that array using the sort filter, which sorts the array by its key.

I should note that this approach creates a criteria model and calls the first method for each user, so it probably causes quite a number of database queries. That's why I recommend to add caching to the template or even think about getting the users with a custom plugin and SQL.

{% set designers = {} %}

{% set users = craft.users.group('designers').limit(null) %}
{% for user in users %}

    {% set latestDesign = craft.entries.section('designs').author(user).first() %}
    {% if latestDesign %}
        {% set designers = designers|merge({
            (latestDesign.postDate): user
        }) %}
    {% endif %}

{% endfor %}

{% for key, designer in designers|sort %}
    <li>{{ designer }} (Latest design posted: {{ key }})</li> 
{% endfor %}
| improve this answer | |

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.