Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-11-03T01:46:04.563Z
Answers.LastActivityDate: 2018-11-03T01:46:04.563Z
Answers.UserDisplayName: Simon Kuran
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-16T19:06:50.103Z
Answers.Text: 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.
Answers.UserDisplayName: August Miller
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-18T16:09:30.330Z
Answers.Text: 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
Answers.UserDisplayName: iamkeir
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-16T18:40:21.877Z
Answers.LastActivityDate: 2020-03-16T18:40:21.877Z
Answers.UserDisplayName: iamkeir
Body: <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>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-08T10:24:00.720Z
Comments.Text: Is this in the CMS or on the front end?
Comments.UserDisplayName: Jason Mayo
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-10-08T15:02:46.163Z
Comments.Text: @JasonMayo front end.
Comments.UserDisplayName: Simon Kuran
Tags: <templating><craft3><plugin-craftcommerce><query><plugin-craftcommerce2>
Title: How to get all users with active Commerce 2 subscription
ThreadText: # 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**: @username_1 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() %}
  
  <ul>
    {% for user in users %}
      <li>{{ user.username }}</li>
    {% endfor %}
  </ul>
  
  ```
  
  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 @username_3 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  
  
  ---
  
  