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?


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


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".

  • 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? – Mats Mikkel Rummelhoff Jun 10 '16 at 9:09
  • 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 Jun 10 '16 at 9:12
  • 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 Jun 10 '16 at 10:15
  • 2
    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 Jun 10 '16 at 10:18
  • That's the answer, and that's why this code wasn't working yesterday when I tried! Thanks! – Matt Wilcox Jun 10 '16 at 10:20

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();
  • It was the null status that was throwing me; didn't know about it! Thanks. – Matt Wilcox Jun 10 '16 at 10:27

This should do it:

$entryCriteria = craft()->elements->getCriteria(ElementType::Entry);
$entryCriteria->authorId = 10;
$entryModels = $entryCriteria->find();
  • Thanks, but this does not seem to work :/ Edited my question to show example code... – Matt Wilcox Jun 10 '16 at 8:26

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.