Answers.Body: <p>While this does work, the <code>User::findAll()</code> method is just a wrapper around the <code>User::find()</code> method, which creates a <code>UserQuery</code>. I would prefer using a <code>UserQuery</code> directly, since you get more autocompletion and type-safety compared to criteria arrays. Another issue is using <code>count()</code> on the query directly, without executing it first. This is less efficient, since the query is executed behind the scenes, and a potential source of bugs and confusion.</p>
  <p>A more idiomatic way to write your query would be this:</p>
  <pre class="lang-php prettyprint-override"><code>use craft\elements\User;
  
  return User::find()
      -&gt;customField('some string')
      -&gt;count();
  </code></pre>
  <p>By executing the query with <code>count()</code>, the database only returns the count, not all the data for all the users. This is more efficient if you don't need the actual users.</p>
  <p>If you need the user objects as well, I would execute the query first and then count the resulting array:</p>
  <pre class="lang-php prettyprint-override"><code>$users = User::find()
      -&gt;customField('some string')
      -&gt;all();
  $count = count($users);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-11T19:15:35.907Z
Answers.Text: Great, thanks for the tip!
Answers.UserDisplayName: Dario Zadro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-01-10T09:19:43.940Z
Answers.LastActivityDate: 2024-01-10T09:19:43.940Z
Answers.UserDisplayName: MoritzLost
Body: <p>Spent a good amount of time trying to return all Users that match a custom field value.</p>
  <p>Reading the <a href="https://docs.craftcms.com/api/v4/craft-base-element.html#method-findall" rel="nofollow noreferrer">docs here</a>, it shows a <code>findAll()</code> command.</p>
  <p>However, when trying the code below, it's not working.</p>
  <pre><code>use craft\elements\User;
  
  $value = 'some string';
  
  $users = User::findAll(['customField' =&gt; $value]);
  
  return count($users);  
  </code></pre>
  <p>The query doesn't fail, but rather, it times out. If I put in a non-valid custom field, an error is thrown.</p>
  <p>Any ideas how to get a count of all <strong>Users</strong> that match a custom field value?</p>
  <p>EDIT: This actually does work as expected! My code included a do/while loop, which was the issue. Leaving the questions up in case someone else is searching for this or has a different solution.</p>
  
Tags: <plugin-development><users><custom-field>
Title: Find all Users with a Custom Field Value in a Plugin
ThreadText: # Find all Users with a Custom Field Value in a Plugin
  
  (Asked by: username_0 on 2024-01-09)
  
  Spent a good amount of time trying to return all Users that match a custom field value.
  
  Reading the [docs here](https://docs.craftcms.com/api/v4/craft-base-element.html#method-findall), it shows a `findAll()` command.
  
  However, when trying the code below, it's not working.
  
  ```
  use craft\elements\User;
  
  $value = 'some string';
  
  $users = User::findAll(['customField' => $value]);
  
  return count($users);  
  
  ```
  
  The query doesn't fail, but rather, it times out. If I put in a non-valid custom field, an error is thrown.
  
  Any ideas how to get a count of all **Users** that match a custom field value?
  
  EDIT: This actually does work as expected! My code included a do/while loop, which was the issue. Leaving the questions up in case someone else is searching for this or has a different solution.
  
  **Tags:** plugin-development, users, custom-field
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-01-10. Score: 2
  
  While this does work, the `User::findAll()` method is just a wrapper around the `User::find()` method, which creates a `UserQuery`. I would prefer using a `UserQuery` directly, since you get more autocompletion and type-safety compared to criteria arrays. Another issue is using `count()` on the query directly, without executing it first. This is less efficient, since the query is executed behind the scenes, and a potential source of bugs and confusion.
  
  A more idiomatic way to write your query would be this:
  
  ```php
  use craft\elements\User;
  
  return User::find()
      ->customField('some string')
      ->count();
  
  ```
  
  By executing the query with `count()`, the database only returns the count, not all the data for all the users. This is more efficient if you don't need the actual users.
  
  If you need the user objects as well, I would execute the query first and then count the resulting array:
  
  ```php
  $users = User::find()
      ->customField('some string')
      ->all();
  $count = count($users);
  
  ```
  
  > **Comments:**
  > **username_0**: Great, thanks for the tip!  
  
  ---
  
  