<p>Fields are related to entry types via their field layout. </p>

<p>You need to find all field layouts your field is attached to then find entry types using these field layouts. Once you have that, fetch all your entries by types.</p>

<p>If you already know your field's <code>id</code>, you don't need this but to find the <code>id</code> of your field by its handle:</p>

<pre class="lang-php prettyprint-override"><code>$fieldId = Craft::$app-&gt;getFields()-&gt;getFieldByHandle('yourfieldHandle')-&gt;id;
</code></pre>

<p>Get all field layouts using that field:</p>

<pre class="lang-php prettyprint-override"><code>$fieldLayouts = (new Query())
    -&gt;select(['layoutId'])
    -&gt;from(['{{%fieldlayoutfields}}'])
    -&gt;where(Db::parseParam('fieldId', $fieldId))
    -&gt;column();
</code></pre>

<p>Get all entry types using the field layouts you've just fetched:</p>

<pre class="lang-php prettyprint-override"><code>$entryTypes = (new Query())
    -&gt;select(['handle'])
    -&gt;from([Table::ENTRYTYPES])
    -&gt;where(Db::parseParam('fieldLayoutId', $fieldLayouts))
    -&gt;column();
</code></pre>

<p>Or you can do both the above at once (get entry types by field id):</p>

<pre class="lang-php prettyprint-override"><code>$entryTypes = (new Query())
    -&gt;select(['entrytypes.handle'])
    -&gt;from(['{{%entrytypes}} entrytypes'])
    -&gt;innerJoin('{{%fieldlayoutfields}} fieldlayoutfields', '[[entrytypes.fieldLayoutId]] = [[fieldlayoutfields.layoutId]]')
    -&gt;where(Db::parseParam('fieldlayoutfields.fieldId', $fieldId))
    -&gt;column();
</code></pre>

<p>Once you have your entry types, get all entries matching:</p>

<pre class="lang-php prettyprint-override"><code>$entries = Entry::find()-&gt;type($entryTypes)-&gt;all();
</code></pre>

<p>Altogether:</p>

<pre class="lang-php prettyprint-override"><code>use Craft;
use craft\db\Query;
use craft\elements\Entry;

... 

// Fetch your field by its handle, if you know the id, just manually set it here
$fieldId = Craft::$app-&gt;getFields()-&gt;getFieldByHandle('yourFieldHandle')-&gt;id;

// Find all entryTypes using the fieldLayouts your field is attached to
$entryTypes = (new Query())
    -&gt;select(['entrytypes.handle'])
    -&gt;from(['{{%entrytypes}} entrytypes'])
    -&gt;innerJoin('{{%fieldlayoutfields}} fieldlayoutfields', '[[entrytypes.fieldLayoutId]] = [[fieldlayoutfields.layoutId]]')
    -&gt;where(Db::parseParam('fieldlayoutfields.fieldId', $fieldId))
    -&gt;column();

// Find all entries by entryType
$entries = Entry::find()-&gt;type($entryTypes)-&gt;all();
</code></pre>

CC BY-SA 4.0
2020-05-09T12:43:58.587Z
2020-05-09T12:43:58.587Z
Oli
<p>I'm developing a plugin and I'm searching a way to get all entries where a field exists (by id or handle).
I'm trying to get all the entries even if the value of the field is null, empty or text.</p>

<p>I can't find a way to that. Can somebody help ?</p>

<plugin-development>
Get all elements (entries) where a field exists
# Get all elements (entries) where a field exists

(Asked by: username_0 on 2020-05-08)

I'm developing a plugin and I'm searching a way to get all entries where a field exists (by id or handle).
I'm trying to get all the entries even if the value of the field is null, empty or text.

I can't find a way to that. Can somebody help ?

**Tags:** plugin-development


---

## 1 answers

### Answer by username_1 on 2020-05-09. Score: 1 | Accepted answer

Fields are related to entry types via their field layout. 
You need to find all field layouts your field is attached to then find entry types using these field layouts. Once you have that, fetch all your entries by types.
If you already know your field's id, you don't need this but to find the id of your field by its handle:
$fieldId = Craft::$app->getFields()->getFieldByHandle('yourfieldHandle')->id;

Get all field layouts using that field:
$fieldLayouts = (new Query())
    ->select(['layoutId'])
    ->from(['{{%fieldlayoutfields}}'])
    ->where(Db::parseParam('fieldId', $fieldId))
    ->column();

Get all entry types using the field layouts you've just fetched:
$entryTypes = (new Query())
    ->select(['handle'])
    ->from([Table::ENTRYTYPES])
    ->where(Db::parseParam('fieldLayoutId', $fieldLayouts))
    ->column();

Or you can do both the above at once (get entry types by field id):
$entryTypes = (new Query())
    ->select(['entrytypes.handle'])
    ->from(['{{%entrytypes}} entrytypes'])
    ->innerJoin('{{%fieldlayoutfields}} fieldlayoutfields', '[[entrytypes.fieldLayoutId]] = [[fieldlayoutfields.layoutId]]')
    ->where(Db::parseParam('fieldlayoutfields.fieldId', $fieldId))
    ->column();

Once you have your entry types, get all entries matching:
$entries = Entry::find()->type($entryTypes)->all();

Altogether:
use Craft;
use craft\db\Query;
use craft\elements\Entry;

... 

// Fetch your field by its handle, if you know the id, just manually set it here
$fieldId = Craft::$app->getFields()->getFieldByHandle('yourFieldHandle')->id;

// Find all entryTypes using the fieldLayouts your field is attached to
$entryTypes = (new Query())
    ->select(['entrytypes.handle'])
    ->from(['{{%entrytypes}} entrytypes'])
    ->innerJoin('{{%fieldlayoutfields}} fieldlayoutfields', '[[entrytypes.fieldLayoutId]] = [[fieldlayoutfields.layoutId]]')
    ->where(Db::parseParam('fieldlayoutfields.fieldId', $fieldId))
    ->column();

// Find all entries by entryType
$entries = Entry::find()->type($entryTypes)->all();


---

