<p>Here's how you'd do it making use of the <code>relatedTo</code> parameter</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
$criteria-&gt;slug = 'white';
$category = $criteria-&gt;first();

$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;relatedTo = array(
    'targetElement' =&gt; $category
);
$entries = $criteria-&gt;find();
</code></pre>

CC BY-SA 3.0
2016-03-30T19:25:40.570Z
Thanks man. We seem to have posted almost at the same time :)
Nobster
CC BY-SA 3.0
2017-05-19T21:02:28.800Z
'targetElement' = $category should be 'targetElement' => $category
Brad
CC BY-SA 3.0
2016-03-30T19:22:02.047Z
2017-05-28T12:59:16.927Z
2017-05-28T12:59:16.927Z
carlcs
<p>Here's another solution which feels more correct. It uses relations and the relatedTo parameter.</p>

<pre><code>// First get the all CategoryModels
$colorCriteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Category);
$colorCriteria-&gt;group = 'colors';
$colorCriteria-&gt;slug = array("white");
$colorCategories = $colorCriteria-&gt;find();

// Then get the entries
$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = "houses";
$criteria-&gt;size = "big";
$criteria-&gt;relatedTo = $colorCategories;//&lt;= Here's the fancy part. 
$entries = craft()-&gt;elements-&gt;buildElementsQuery($criteria)-&gt;queryAll();
</code></pre>

<p>Hopefully that helps someone else. </p>

CC BY-SA 3.0
2016-08-29T14:10:35.247Z
So this will find all entries that has white set at color?
Spoeken
CC BY-SA 3.0
2016-03-30T19:23:06.067Z
2016-03-30T19:23:06.067Z
Nobster
<p>Okey. I found a solution. Probably not the fastest because of a search but it works.</p>

<pre><code>$criteria-&gt;search = "color:white";
</code></pre>

<p>So color is the field alias. white is the category handle to look for.</p>

<p>Read more about search here => <a href="https://craftcms.com/docs/searching" rel="nofollow">https://craftcms.com/docs/searching</a></p>

CC BY-SA 3.0
2016-03-30T17:01:17.743Z
2016-03-30T17:01:17.743Z
Nobster
<p>I need help.</p>

<p>I want to get entries based on multiple field search. It works for basic field types like selectbox or plain text but I can't get it to work with Categories Fields.</p>

<p>The field size is a selectbox field and works good.
The color is a Categories field and doesn't work.</p>

<p>The code:</p>

<pre><code>$criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
$criteria-&gt;section = "houses";
$criteria-&gt;size = "big"; 
$criteria-&gt;color = "white"; // &lt;== HOW???
$entries = craft()-&gt;elements-&gt;buildElementsQuery($criteria)-&gt;queryAll();
</code></pre>

<p>How do I get entries with a specific Categories Field value?</p>

<p>Thanks for any help.</p>

<categories>
Get entries by category value from within plugin
# Get entries by category value from within plugin

(Asked by: username_0 on 2016-03-30)

I need help.
I want to get entries based on multiple field search. It works for basic field types like selectbox or plain text but I can't get it to work with Categories Fields.
The field size is a selectbox field and works good.
The color is a Categories field and doesn't work.
The code:
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = "houses";
$criteria->size = "big"; 
$criteria->color = "white"; // <== HOW???
$entries = craft()->elements->buildElementsQuery($criteria)->queryAll();

How do I get entries with a specific Categories Field value?
Thanks for any help.

**Tags:** categories

---

## 3 answers

### Answer by username_1 on 2016-03-30. Score: 3 | Accepted answer

Here's how you'd do it making use of the relatedTo parameter
$criteria = craft()->elements->getCriteria(ElementType::Category);
$criteria->slug = 'white';
$category = $criteria->first();

$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->relatedTo = array(
    'targetElement' => $category
);
$entries = $criteria->find();

> **Comments:**
> **username_0**: Thanks man. We seem to have posted almost at the same time :)  
> **username_2**: 'targetElement' = $category should be 'targetElement' => $category  

---

### Answer by username_0 on 2016-03-30. Score: 4

Here's another solution which feels more correct. It uses relations and the relatedTo parameter.
// First get the all CategoryModels
$colorCriteria = craft()->elements->getCriteria(ElementType::Category);
$colorCriteria->group = 'colors';
$colorCriteria->slug = array("white");
$colorCategories = $colorCriteria->find();

// Then get the entries
$criteria = craft()->elements->getCriteria(ElementType::Entry);
$criteria->section = "houses";
$criteria->size = "big";
$criteria->relatedTo = $colorCategories;//<= Here's the fancy part. 
$entries = craft()->elements->buildElementsQuery($criteria)->queryAll();

Hopefully that helps someone else.

> **Comments:**
> **username_3**: So this will find all entries that has white set at color?  

---

### Answer by username_0 on 2016-03-30. Score: 1

Okey. I found a solution. Probably not the fastest because of a search but it works.
$criteria->search = "color:white";

So color is the field alias. white is the category handle to look for.
Read more about search here => https://craftcms.com/docs/searching


---

