<p>This should do it:</p>

<pre><code>$entryCriteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$entryCriteria-&gt;authorId = 10;
$entryModels = $entryCriteria-&gt;find();
</code></pre>

CC BY-SA 3.0
2016-06-10T08:26:23.547Z
Thanks, but this does not seem to work :/ Edited my question to show example code...
Matt Wilcox
CC BY-SA 3.0
2016-06-09T16:47:27.453Z
2016-06-09T16:47:27.453Z
Brad Bell
<p>Getting entries in plugins is slightly different from within templates and you should read the details on <a href="https://craftcms.com/docs/plugins/working-with-elements" rel="nofollow">Working with Elements</a> in a plugin in the Craft docs.</p>

<p>Taking that as a guide, you need to create an <code>ElementCriteriaModel</code> instance which selects the entries you are interested in:</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;authorId = 10;
</code></pre>

<p>Note that the <code>ElementCriteriaModel</code> won't pull disabled entries by default. If you want to pull both enabled and disabled entries, you can add the <code>status</code> parameter to your query:</p>

<pre><code>$criteria-&gt;status = null;
</code></pre>

<p>Finally, you need to call <code>find()</code> on the <code>ElementCriteriaModel</code> to fetch the entries matching your criteria:</p>

<pre><code>$entries = $criteria-&gt;find();
</code></pre>

CC BY-SA 3.0
2016-06-10T10:27:51.413Z
It was the null status that was throwing me; didn't know about it! Thanks.
Matt Wilcox
CC BY-SA 3.0
2016-06-09T16:52:18.007Z
2016-06-10T10:22:18.743Z
2016-06-10T10:22:18.743Z
Steve Rowling
<p>This seems pretty simple in a template; along the lines of</p>

<pre><code>if( craft.entries.authorId(10) | length ){ ... }
</code></pre>

<p>But how do I do that from a plugin?</p>

<pre><code>craft-&gt;entries-&gt;authorId(10)
</code></pre>

<p>doesn't seem to exist and I've been banging my head against the docs not knowing what I'm actually looking for...</p>

<p>[EDIT]</p>

<p>I now have the following code:</p>

<pre><code>$entryCriteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$entryCriteria-&gt;authorId = $existingUser-&gt;id;
$existingPages = $entryCriteria-&gt;find();

if ($existingPages) {
    AutoUserEntryPlugin::log('User already exists and has a page belonging to them; aborting.', LogLevel::Info);
    throw new Exception(Craft::t("You already have an account and a page created for you. Aborting. You have not been charged."));
} else {
    AutoUserEntryPlugin::log('User with ID ' . $existingUser-&gt;id . ' does not seem to have a page.', LogLevel::Info);
}
</code></pre>

<p>But with a user I know for a fact has an Entry for which they are the author, it's returning "User with ID 1198 does not seem to have a page".</p>

CC BY-SA 3.0
2016-06-10T09:09:38.247Z
Both Steve and Brad's answers (essentially the exact same code) should work, so there must be something amiss. What's the status of the entry – is it disabled or enabled?
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-06-10T09:12:08.430Z
Also, double check if the user with an ID `1198` has in fact authored any entries – easily done by checking for the integer `1198` in the `authorId` column in the `craft_entries` database table.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-06-10T10:15:37.797Z
The entry is disabled!

OK, so I need to add a new criteria to say ignore the status... trying to figure that out now, thanks!
Matt Wilcox
CC BY-SA 3.0
2016-06-10T10:18:28.227Z
If the entry is disabled, that's your issue. Be advised that the `$entryCriteria->find` won't pull disabled entries by default, but you can add `$entryCriteria->status = null;` to the query to also include disabled entries.
Mats Mikkel Rummelhoff
CC BY-SA 3.0
2016-06-10T10:20:07.810Z
That's the answer, and that's why this code wasn't working yesterday when I tried! Thanks!
Matt Wilcox
CC BY-SA 3.0
2016-06-10T10:20:52.957Z
Awesome. I'll edit Steve's answer to include that detail.
Mats Mikkel Rummelhoff
<plugin-development>
How to check if a user has any entries they're an author of... in a plugin?
# How to check if a user has any entries they're an author of... in a plugin?

(Asked by: username_0 on 2016-06-09)

This seems pretty simple in a template; along the lines of
if( craft.entries.authorId(10) | length ){ ... }

But how do I do that from a plugin?
craft->entries->authorId(10)

doesn't seem to exist and I've been banging my head against the docs not knowing what I'm actually looking for...
[EDIT]
I now have the following code:
$entryCriteria = craft()->elements->getCriteria(ElementType::Entry);
$entryCriteria->authorId = $existingUser->id;
$existingPages = $entryCriteria->find();

if ($existingPages) {
    AutoUserEntryPlugin::log('User already exists and has a page belonging to them; aborting.', LogLevel::Info);
    throw new Exception(Craft::t("You already have an account and a page created for you. Aborting. You have not been charged."));
} else {
    AutoUserEntryPlugin::log('User with ID ' . $existingUser->id . ' does not seem to have a page.', LogLevel::Info);
}

But with a user I know for a fact has an Entry for which they are the author, it's returning "User with ID 1198 does not seem to have a page".

**Tags:** plugin-development

> **Comments:**
> **username_1**: Both Steve and Brad's answers (essentially the exact same code) should work, so there must be something amiss. What's the status of the entry – is it disabled or enabled?  
> **username_1**: Also, double check if the user with an ID `1198` has in fact authored any entries – easily done by checking for the integer `1198` in the `authorId` column in the `craft_entries` database table.  
> **username_0**: The entry is disabled!  OK, so I need to add a new criteria to say ignore the status... trying to figure that out now, thanks!  
> **username_1**: If the entry is disabled, that's your issue. Be advised that the `$entryCriteria->find` won't pull disabled entries by default, but you can add `$entryCriteria->status = null;` to the query to also include disabled entries.  
> **username_0**: That's the answer, and that's why this code wasn't working yesterday when I tried! Thanks!  
> **username_1**: Awesome. I'll edit Steve's answer to include that detail.  

---

## 2 answers

### Answer by username_3 on 2016-06-09. Score: 4 | Accepted answer

Getting entries in plugins is slightly different from within templates and you should read the details on Working with Elements in a plugin in the Craft docs.
Taking that as a guide, you need to create an ElementCriteriaModel instance which selects the entries you are interested in:
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->authorId = 10;

Note that the ElementCriteriaModel won't pull disabled entries by default. If you want to pull both enabled and disabled entries, you can add the status parameter to your query:
$criteria->status = null;

Finally, you need to call find() on the ElementCriteriaModel to fetch the entries matching your criteria:
$entries = $criteria->find();

> **Comments:**
> **username_0**: It was the null status that was throwing me; didn't know about it! Thanks.  

---

### Answer by username_2 on 2016-06-09. Score: 2

This should do it:
$entryCriteria = craft()->elements->getCriteria(ElementType::Entry);
$entryCriteria->authorId = 10;
$entryModels = $entryCriteria->find();

> **Comments:**
> **username_0**: Thanks, but this does not seem to work :/ Edited my question to show example code...  

---

