Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-30T19:25:40.570Z
Answers.Text: Thanks man. We seem to have posted almost at the same time :)
Answers.UserDisplayName: Nobster
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-19T21:02:28.800Z
Answers.Text: 'targetElement' = $category should be 'targetElement' => $category
Answers.UserDisplayName: Brad
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-30T19:22:02.047Z
Answers.LastActivityDate: 2017-05-28T12:59:16.927Z
Answers.LastEditDate: 2017-05-28T12:59:16.927Z
Answers.UserDisplayName: carlcs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-29T14:10:35.247Z
Answers.Text: So this will find all entries that has white set at color?
Answers.UserDisplayName: Spoeken
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-30T19:23:06.067Z
Answers.LastActivityDate: 2016-03-30T19:23:06.067Z
Answers.UserDisplayName: Nobster
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-03-30T17:01:17.743Z
Answers.LastActivityDate: 2016-03-30T17:01:17.743Z
Answers.UserDisplayName: Nobster
Body: <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>
  
Tags: <categories>
Title: Get entries by category value from within plugin
ThreadText: # 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>
  
  
  ---
  
  