<p>The correct syntax or finding <em>all</em> users regardless of status is <code>status(null)</code>, which you've tried, but the issue isn't your status parameter, it's the <code>targetElement</code> parameter of your <code>relatedTo</code> query.</p>

<p>It only accepts the actual element you're looking to query, not its ID.</p>

<p>If you already have the target element object, you can just pass that in. If you don't, you can retrieve it with:</p>

<pre><code>$targetElement = Craft::$app-&gt;getElements()-&gt;getElementById($organizationId);
</code></pre>

<p>Then use:</p>

<pre><code>array(
    'targetElement' =&gt; $targetElement, 
    'field'         =&gt; 'organisation'
)
</code></pre>

<p>Relevant docs here: <a href="https://docs.craftcms.com/v3/relations.html#the-relatedto-parameter" rel="nofollow noreferrer">https://docs.craftcms.com/v3/relations.html#the-relatedto-parameter</a></p>

CC BY-SA 4.0
2018-11-20T18:35:36.480Z
I have actually switched to using template tags now, as I figured out how to still retrieve the entries without the plugin. In my template tags I am passing the element object organisation not its ID, but still cant get it. Will update my code to show the template code.
Laurence Cope
CC BY-SA 4.0
2018-11-20T18:46:17.650Z
I got it working OK using .status(null) in the template tags. But good to know its because of passing an ID and not an object when using PHP. I am just unable to test due to changing logic.
Laurence Cope
CC BY-SA 4.0
2018-11-20T03:24:44.003Z
2018-11-20T03:24:44.003Z
Brad Bell
<p>My users have a relationship field called Organisation.</p>

<p>In my plugin, I am trying to fetch all users that are related to the organisation I pass it.</p>

<p>BUT I would like ALL users including Pending, so those who have not verified their email and set a password yet. </p>

<p>I am using this code but it only fetches the verified users, not pending. </p>

<pre><code>$entries = craft\elements\User::find();

$relatedEntries = $entries-&gt;relatedTo(
    array(
        'targetElement' =&gt; $organisationId, 
        'field'         =&gt; 'organisation'
    )
);

// Fetch all entries
$relatedEntries = $relatedEntries-&gt;all(); 
</code></pre>

<p>I have also tried using combinations of ->status(null), e.g. </p>

<pre><code>$entries = craft\elements\User::find();

    $relatedEntries = $entries-&gt;relatedTo(
        array(
            'targetElement' =&gt; $organisationId, 
            'field'         =&gt; 'organisation'
        )
    );

    // Fetch all entries
    $relatedEntries = $relatedEntries-&gt;status(null)-&gt;all();
</code></pre>

<p>or</p>

<pre><code>$entries = craft\elements\User::find()-&gt;status(null);

    $relatedEntries = $entries-&gt;relatedTo(
        array(
            'targetElement' =&gt; $organisationId, 
            'field'         =&gt; 'organisation'
        )
    );

    // Fetch all entries
    $relatedEntries = $relatedEntries-&gt;all();
</code></pre>

<p>But they always just show all verified users. </p>

<p>Thanks</p>

<p><strong>Edit</strong></p>

<p>I have changed my logic and database setup a bit, and so decided to try using just template tags now. And so I got it working OK using .status(null with template tags. </p>

<pre><code>{% set organisations = craft.entries().section('suppliers').authorId(currentUser.id) %}

{% for organisation in organisations %}

    {% set users = craft.users().relatedTo(organisation).status(null %}
    {% for user in users %}
        {{user.firstName}} {{user.lastName}} - {{user.status}}&lt;br/&gt;
    {% endfor%}

{% endfor%}
</code></pre>

<craft3><relatedto>
How to fetch ALL users related to a field, including unverified/pending?
# How to fetch ALL users related to a field, including unverified/pending?

(Asked by: username_0 on 2018-11-18)

My users have a relationship field called Organisation.
In my plugin, I am trying to fetch all users that are related to the organisation I pass it.
BUT I would like ALL users including Pending, so those who have not verified their email and set a password yet. 
I am using this code but it only fetches the verified users, not pending. 
$entries = craft\elements\User::find();

$relatedEntries = $entries->relatedTo(
    array(
        'targetElement' => $organisationId, 
        'field'         => 'organisation'
    )
);

// Fetch all entries
$relatedEntries = $relatedEntries->all(); 

I have also tried using combinations of ->status(null), e.g. 
$entries = craft\elements\User::find();

    $relatedEntries = $entries->relatedTo(
        array(
            'targetElement' => $organisationId, 
            'field'         => 'organisation'
        )
    );

    // Fetch all entries
    $relatedEntries = $relatedEntries->status(null)->all();

or
$entries = craft\elements\User::find()->status(null);

    $relatedEntries = $entries->relatedTo(
        array(
            'targetElement' => $organisationId, 
            'field'         => 'organisation'
        )
    );

    // Fetch all entries
    $relatedEntries = $relatedEntries->all();

But they always just show all verified users. 
Thanks
Edit
I have changed my logic and database setup a bit, and so decided to try using just template tags now. And so I got it working OK using .status(null with template tags. 
{% set organisations = craft.entries().section('suppliers').authorId(currentUser.id) %}

{% for organisation in organisations %}

    {% set users = craft.users().relatedTo(organisation).status(null %}
    {% for user in users %}
        {{user.firstName}} {{user.lastName}} - {{user.status}}
    {% endfor%}

{% endfor%}

**Tags:** craft3, relatedto

---

## 1 answers

### Answer by username_1 on 2018-11-20. Score: 1

The correct syntax or finding all users regardless of status is status(null), which you've tried, but the issue isn't your status parameter, it's the targetElement parameter of your relatedTo query.
It only accepts the actual element you're looking to query, not its ID.
If you already have the target element object, you can just pass that in. If you don't, you can retrieve it with:
$targetElement = Craft::$app->getElements()->getElementById($organizationId);

Then use:
array(
    'targetElement' => $targetElement, 
    'field'         => 'organisation'
)

Relevant docs here: https://docs.craftcms.com/v3/relations.html#the-relatedto-parameter

> **Comments:**
> **username_0**: I have actually switched to using template tags now, as I figured out how to still retrieve the entries without the plugin. In my template tags I am passing the element object organisation not its ID, but still cant get it. Will update my code to show the template code.  
> **username_0**: I got it working OK using .status(null) in the template tags. But good to know its because of passing an ID and not an object when using PHP. I am just unable to test due to changing logic.  

---

