<p>I solved this by creating a simple plugin.</p>

<pre><code>use craft\db\Query;

class MyPluginVariable
{
      public function allActivelySubscribedUserIds()
      {
           $subscribedUserIds = (new Query())
                -&gt;select('userId')
                -&gt;from(['{{%commerce_subscriptions}}'])
                -&gt;distinct()
                -&gt;all();

            // Create array of just user IDs
            $subscribedUserIds = array_column($subscribedUserIds, 'userId');

            return $subscribedUserIds;
       }
 }
</code></pre>

<p>I my case I needed to display all entries authored by users that are currently subscribed. In my template I used the variable like this:</p>

<pre><code>{% set subscribedUserIds = craft.MyPlugin.allActivelySubscribedUserIds %}

{% set entries = craft.entries()
    .authorId(subscribedUserIds)
    .all()
%}
</code></pre>

CC BY-SA 4.0
2018-11-03T01:46:04.563Z
2018-11-03T01:46:04.563Z
Simon Kuran
<p>Not Commerce 2, but in Commerce 3, like this:</p>

<pre><code>{% set userIdsWithSubscriptions = craft.subscriptions.limit(null).all()|group('userId')|keys %}
{% set users = craft.users.id(userIdsWithSubscriptions).all() %}

&lt;ul&gt;
  {% for user in users %}
    &lt;li&gt;{{ user.username }}&lt;/li&gt;
  {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>Via Nathaniel @ Craft Support (thanks!)</p>

CC BY-SA 4.0
2020-03-16T19:06:50.103Z
FYI: this may need some optimization, depending on the scale of your application, like selection of specific columns (`.select('userId').column()`, instead of `.all() | group('userId') | keys`), as loading all Element data + instantiating the models can quickly exhaust memory—we've seen major slowdowns after about 1K elements, and out-of-memory errors at 10K.
August Miller
CC BY-SA 4.0
2020-03-18T16:09:30.330Z
Thanks @AugustMiller that's good to add in. I am using a mixture of `.with` and pagination, but I think I may add in the `.select` too
iamkeir
CC BY-SA 4.0
2020-03-16T18:40:21.877Z
2020-03-16T18:40:21.877Z
iamkeir
<p>How do I get all the users with an active subscription in Commerce 2? I want to be able to filter entries by subscribed users. </p>

CC BY-SA 4.0
2018-10-08T10:24:00.720Z
Is this in the CMS or on the front end?
Jason Mayo
CC BY-SA 4.0
2018-10-08T15:02:46.163Z
@JasonMayo front end.
Simon Kuran
<templating><craft3><plugin-craftcommerce><query><plugin-craftcommerce2>
How to get all users with active Commerce 2 subscription
# How to get all users with active Commerce 2 subscription

(Asked by: username_0 on 2018-10-04)

How do I get all the users with an active subscription in Commerce 2? I want to be able to filter entries by subscribed users.

**Tags:** templating, craft3, plugin-craftcommerce, query, plugin-craftcommerce2

> **Comments:**
> **username_1**: Is this in the CMS or on the front end?  
> **username_0**: @JasonMayo front end.  

---

## 2 answers

### Answer by username_0 on 2018-11-03. Score: 1 | Accepted answer

I solved this by creating a simple plugin.
use craft\db\Query;

class MyPluginVariable
{
      public function allActivelySubscribedUserIds()
      {
           $subscribedUserIds = (new Query())
                ->select('userId')
                ->from(['{{%commerce_subscriptions}}'])
                ->distinct()
                ->all();

            // Create array of just user IDs
            $subscribedUserIds = array_column($subscribedUserIds, 'userId');

            return $subscribedUserIds;
       }
 }

I my case I needed to display all entries authored by users that are currently subscribed. In my template I used the variable like this:
{% set subscribedUserIds = craft.MyPlugin.allActivelySubscribedUserIds %}

{% set entries = craft.entries()
    .authorId(subscribedUserIds)
    .all()
%}


---

### Answer by username_2 on 2020-03-16. Score: 1

Not Commerce 2, but in Commerce 3, like this:
{% set userIdsWithSubscriptions = craft.subscriptions.limit(null).all()|group('userId')|keys %}
{% set users = craft.users.id(userIdsWithSubscriptions).all() %}


  {% for user in users %}
    {{ user.username }}
  {% endfor %}


Via Nathaniel @ Craft Support (thanks!)

> **Comments:**
> **username_3**: FYI: this may need some optimization, depending on the scale of your application, like selection of specific columns (`.select('userId').column()`, instead of `.all() | group('userId') | keys`), as loading all Element data + instantiating the models can quickly exhaust memory—we've seen major slowdowns after about 1K elements, and out-of-memory errors at 10K.  
> **username_2**: Thanks @AugustMiller that's good to add in. I am using a mixture of `.with` and pagination, but I think I may add in the `.select` too  

---

