1

I am looking for a programatic way to access an entry by an attribute in PHP, using Craft CMS 3.

Here is what I have so far:

Craft::$app->getEntries()->getEntryById('1234');

getEntryById() seems to be the only method available off of getEntries() ...

What I would like to do (in pseudo code):

Craft::$app->getEntries()->getEntryByAttribute('ItemNumber', '1234');
ryanpcmcquen
  • 6,285
  • 3
  • 24
  • 37

1 Answers1

3

You can query an Entry by field value like this:

\craft\elements\Entry::find()->section('mySection')->where(['field_myFieldHandle' => $myValue])->one();
Tony DeStefano
  • 819
  • 6
  • 11
  • Thanks for this. By the way, what is the difference between `Craft::$app->getEntries()` and `\craft\elements\Entry::find()`? Is one preferential over another? – iamkeir Dec 11 '21 at 16:02