I would like to get user query in my custom PHP plugin.

According to this document, the code would be like this. https://docs.craftcms.com/v3/dev/element-queries/user-queries.html#example

$queryAllUsers_email = User::find()->email($new_email);
if (count($queryAllUsers_email) > 0) {
    // ...

User means Craft\Elements\User class.

However, the count is 0.

It would be grateful for me if you tell me what is wrong.

  • Try adding ->all() after email($new_email) as per the example
    – Oli
    Jul 11 '19 at 15:45
  • I tried, but the result is same.
    – Kota.M
    Jul 11 '19 at 15:49
  • 1
    Not related to the question, but you can use ".count()" instead of using ".all()" and count() the result. This saves lots of query time. Jul 12 '19 at 9:39

Try this:

$queryAllUsers_email = User::find()

It's likely the email address you're searching for is for a user that isn't activated yet or suspended, etc.

Using anyStatus will search for the user regardless of their status.

  • Thank you, I tried it. However, the count is still 0 although the e-mail address has already been taken.
    – Kota.M
    Jul 12 '19 at 14:21
  • You've verified that $new_email is actually set to an email address that exists in the users table?
    – Brad Bell
    Jul 12 '19 at 21:34
  • I think so, $new_email is the input of e-mail field in the form.
    – Kota.M
    Jul 15 '19 at 16:10

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.