Answers.Body: <p>Would this help?</p>
  
  <p><a href="https://craftcms.stackexchange.com/questions/15251/filter-entries-by-field-value">Filter Entries by Field Value</a></p>
  
  <p>Than it would be something like this I suppose (never used it myself):</p>
  
  <pre><code>{% for item in entry.myCustomField.search('category':'main') %}
      {{ item.category }}
  {% endfor %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-28T16:28:00.707Z
Answers.Text: Thanks for the help. I tried that ( the syntax is 'category:main' rather than 'category' : 'main') but that only seems to work for ElementCriteriaModel instances. Not sure how I can achieve the same thing for a custom type?
Answers.UserDisplayName: James
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-28T18:25:38.367Z
Answers.Text: A long shot here. But have you tried it without the search() function? Just entry.myCustomField('category:main'). I would find it weird that it would work differently with custom fields than it does when using default ones.
Answers.UserDisplayName: Tom De Smet
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-01-25T14:24:27.220Z
Answers.LastActivityDate: 2017-01-25T14:24:27.220Z
Answers.LastEditDate: 2017-04-13T12:50:59.930Z
Answers.UserDisplayName: Tom De Smet
Body: <p>I have a custom fieldtype for Craft which stores its value as an array of Models using AttributeType::Mixed. See below for an example of the FieldType and Model setup </p>
  
  <pre><code>class MyCustomFieldType extends BaseFieldType
  {
      public function defineContentAttribute()
      {
          return AttributeType::Mixed;
      }
  } 
  
  class MyCustomModel extends BaseModel
  {
      protected function defineAttributes()
      {
          return array(
              'url'      =&gt; AttributeType::String,
              'category' =&gt; AttributeType::String
          );
      } 
  }
  </code></pre>
  
  <p>Then when the value is persisted, it's saved as an array, which Craft natively saves as JSON</p>
  
  <pre><code>$values = [new MyCustomModel(), new MyCustomModel(), ...];
  </code></pre>
  
  <p>To display the value in Twig I can do something like</p>
  
  <pre><code>{% for item in entry.myCustomField %}
      {{ item.category }}
  {% endfor %}
  </code></pre>
  
  <p>My question is, how can I only display the elements in my field which have a specific attribute value? For example, if I wanted to display only those items with a category of main, where 'category' is an attribute on my model?</p>
  
  <pre><code>{% for item in entry.myCustomField.category('main') %}
      {{ item.category }}
  {% endfor %}
  </code></pre>
  
Tags: <plugin-development><custom-fieldtype>
Title: Filtering values from Twig for my custom field type
ThreadText: # Filtering values from Twig for my custom field type
  
  (Asked by: username_0 on 2017-01-03)
  
  I have a custom fieldtype for Craft which stores its value as an array of Models using AttributeType::Mixed. See below for an example of the FieldType and Model setup
  
  ```
  class MyCustomFieldType extends BaseFieldType
  {
      public function defineContentAttribute()
      {
          return AttributeType::Mixed;
      }
  } 
  
  class MyCustomModel extends BaseModel
  {
      protected function defineAttributes()
      {
          return array(
              'url'      => AttributeType::String,
              'category' => AttributeType::String
          );
      } 
  }
  
  ```
  
  Then when the value is persisted, it's saved as an array, which Craft natively saves as JSON
  
  ```
  $values = [new MyCustomModel(), new MyCustomModel(), ...];
  
  ```
  
  To display the value in Twig I can do something like
  
  ```
  {% for item in entry.myCustomField %}
      {{ item.category }}
  {% endfor %}
  
  ```
  
  My question is, how can I only display the elements in my field which have a specific attribute value? For example, if I wanted to display only those items with a category of main, where 'category' is an attribute on my model?
  
  ```
  {% for item in entry.myCustomField.category('main') %}
      {{ item.category }}
  {% endfor %}
  
  ```
  
  **Tags:** plugin-development, custom-fieldtype
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-01-25. Score: 1
  
  Would this help?
  
  [Filter Entries by Field Value](https://craftcms.stackexchange.com/questions/15251/filter-entries-by-field-value)
  
  Than it would be something like this I suppose (never used it myself):
  
  ```
  {% for item in entry.myCustomField.search('category':'main') %}
      {{ item.category }}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks for the help. I tried that ( the syntax is 'category:main' rather than 'category' : 'main') but that only seems to work for ElementCriteriaModel instances. Not sure how I can achieve the same thing for a custom type?  
  > **username_1**: A long shot here. But have you tried it without the search() function? Just entry.myCustomField('category:main'). I would find it weird that it would work differently with custom fields than it does when using default ones.  
  
  ---
  
  