<p>You can do the same like with the section, if the query param exists in the field layout </p>

<pre><code>$widgets = Entry::find()-&gt;widgetColor('blue')-&gt;text(':notempty:')-&gt;anotherField(':empty:')-&gt;section('widget')-&gt;all();
</code></pre>

<p>Will search for entries where <code>widgetColor</code> is blue, <code>text</code> is not empty/null and <code>anotherField</code> is empty/null </p>

<p><strong>Edit: just for people who like to understand the behavior</strong></p>

<p>Just to explain you the <code>where</code> parameter in the <code>ElementQuery</code>. The <code>where</code> function uses the Yii2 Query function directly and works directly at the database layer. When you take a look at your <code>content</code> table you'll see all custom fields have a certain prefix (if you didn't change it) so your <code>widgetColor</code> column will look like <code>field_widgetColor</code> when you do</p>

<pre><code>where-&gt;(['widgetColor' =&gt; 'blue');
</code></pre>

<p>it will query</p>

<pre><code>where `content`.`widgetColor` = `blue`
</code></pre>

<p>which will throw an error since the column name is <code>field_widgetColor</code>. The <code>__set</code> function in the <code>Yii/component</code>/ <code>QueryBuilder</code> class will set magic property that exists because of the field handle in your field layout... Or to keep it simple: you can access any "magic" property with the same name like an existing field handle. </p>

<p>Doing </p>

<pre><code>-&gt;widgetColor($property)
</code></pre>

<p>will call the <code>__set</code> function that calls a wrapper that will wrap/attach the field content prefix (<code>field_</code>) to the <code>where</code> function. Thus you can use</p>

<pre><code>where-&gt;(['field_widgetColor' =&gt; 'blue');
</code></pre>

<p>or</p>

<pre><code>where-&gt;([Craft::$app-&gt;getFields()-&gt;oldFieldColumnPrefix . 'widgetColor' =&gt; 'blue');
</code></pre>

CC BY-SA 3.0
2018-03-05T06:21:54.193Z
2018-03-05T07:49:31.440Z
2018-03-05T07:49:31.440Z
Robin Schambach
<p>I need to arbitrarily query entries based on the values of various fields.</p>

<p>For example, if I have an entry type called "Widgets" I can grab them like this:</p>

<pre><code>$widgets = Entry::find()-&gt;section('widget')-&gt;all();
</code></pre>

<p>Now what if widgets have a field called "widgetColor" and I want to select all with a value of "blue" in that field?</p>

<ol>
<li>How can I query for those entries?</li>
<li>Also, how can I query for any entry with no value in that field?</li>
</ol>

<p>I've tried my best to figure it out using the documentation and it seems like method such as <code>where</code> only apply to certain data, such as the title and slug, which are not custom fields on the entry type.</p>

<p>Thanks!</p>

<craft3>
How do you query entries by field values?
# How do you query entries by field values?

(Asked by: username_0 on 2018-03-05)

I need to arbitrarily query entries based on the values of various fields.
For example, if I have an entry type called "Widgets" I can grab them like this:
$widgets = Entry::find()->section('widget')->all();

Now what if widgets have a field called "widgetColor" and I want to select all with a value of "blue" in that field?

How can I query for those entries?
Also, how can I query for any entry with no value in that field?

I've tried my best to figure it out using the documentation and it seems like method such as where only apply to certain data, such as the title and slug, which are not custom fields on the entry type.
Thanks!

**Tags:** craft3

---

## 1 answers

### Answer by username_1 on 2018-03-05. Score: 5

You can do the same like with the section, if the query param exists in the field layout 
$widgets = Entry::find()->widgetColor('blue')->text(':notempty:')->anotherField(':empty:')->section('widget')->all();

Will search for entries where widgetColor is blue, text is not empty/null and anotherField is empty/null 
Edit: just for people who like to understand the behavior
Just to explain you the where parameter in the ElementQuery. The where function uses the Yii2 Query function directly and works directly at the database layer. When you take a look at your content table you'll see all custom fields have a certain prefix (if you didn't change it) so your widgetColor column will look like field_widgetColor when you do
where->(['widgetColor' => 'blue');

it will query
where `content`.`widgetColor` = `blue`

which will throw an error since the column name is field_widgetColor. The __set function in the Yii/component/ QueryBuilder class will set magic property that exists because of the field handle in your field layout... Or to keep it simple: you can access any "magic" property with the same name like an existing field handle. 
Doing 
->widgetColor($property)

will call the __set function that calls a wrapper that will wrap/attach the field content prefix (field_) to the where function. Thus you can use
where->(['field_widgetColor' => 'blue');

or
where->([Craft::$app->getFields()->oldFieldColumnPrefix . 'widgetColor' => 'blue');


---

