Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List all users orderBy number of wishlists #142

Open
chrisrowe opened this issue Aug 1, 2024 · 2 comments
Open

List all users orderBy number of wishlists #142

chrisrowe opened this issue Aug 1, 2024 · 2 comments
Labels

Comments

@chrisrowe
Copy link

Question

I'm working on a dashboard for a client where they'd like to reach out to users with multiple wishlists and lots of saved elements. I can't think of a clean way to do a craft.users query but bring in wishlist count, I wondered if you had any ideas?

Additional context

No response

@chrisrowe chrisrowe changed the title List all users orderBy number of projects List all users orderBy number of wishlists Aug 1, 2024
@engram-design
Copy link
Member

What about something like this:

{% set users = craft.users.all() %}

{% for user in users %}
    {% set lists = craft.wishlist.lists(false, false).userId(user.id).all() %}

    Email: {{ user.email }} has {{ lists | length }} lists

    {% if lists | length > 1 %}
        {% set totalItems = 0 %}

        {% for list in lists %}
            {% set totalItems = totalItems + list.items | length %}
        {% endfor %}

        with {{ totalItems }} items
    {% endif %}

    <br>
{% endfor %}

I've added some text as a bit of a debug, but feel free to adjust to your needs. You'll need to loop through each user, get all their lists, check their list count, then check their total item count.

In terms of wanting to do a single query like "get me all users with X wishlists" that probably can't be done out of the box, other than some direct SQL in PHP/Twig.

@chrisrowe
Copy link
Author

chrisrowe commented Aug 2, 2024

Thanks yeah I was hoping to go the single query route to be able to sort by number of wishlists and then futher query/paginate. I'll come up with something else with some PHP. Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants