Answers.Body: <p>Currently, yes, the only way to modify an element query (when it’s not your own element type) is to do it with a custom field type, from its <a href="http://buildwithcraft.com/classreference/fieldtypes/IFieldType#modifyElementsQuery-detail" rel="nofollow">modifyElementsQuery()</a> function.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As of Craft 2.5, plugins can listen for the <a href="https://craftcms.com/docs/plugins/events-reference#elements-onBeforeBuildElementsQuery" rel="nofollow">elements.onBeforeBuildElementsQuery</a> and <a href="https://craftcms.com/docs/plugins/events-reference#elements-onBuildElementsQuery" rel="nofollow">elements.onBuildElementsQuery</a> events to make changes to the query (or prevent it from getting executed).</p>
  
  <pre class="lang-php prettyprint-override"><code>craft()-&gt;on('elements.buildElementsQuery', function(Event $event) {
      /** @var ElementCriteriaModel $criteria */
      $criteria = $event-&gt;params['criteria'];
  
      /** @var DbCommand $query */
      $query = $event-&gt;params['query'];
  
      if ($criteria-&gt;myCustomParamName) {
          $query-&gt;andWhere( //...
      }
  });
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-08T16:42:56.590Z
Answers.Text: I'm trying to use your suggested `buildElementsQuery` event, but when I use `$criteria->myCustomParamName` it's not passing `myCustomParamName` to my plugin and I'm getting an error in the logfiles `Property "Craft\ElementCriteriaModel.myCustomParamName" is not defined.` I'm trying to use a custom parameter from the elementCriteria in my templates. What am I missing here?
Answers.UserDisplayName: ctigelaar
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-08T21:51:38.937Z
Answers.Text: @Christiaan What version of Craft are you running?
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-09T06:39:55.007Z
Answers.Text: @Brandon I'm using Craft 2.5.2761
Answers.UserDisplayName: ctigelaar
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-10-13T11:30:37.477Z
Answers.LastActivityDate: 2015-12-05T15:03:26.123Z
Answers.LastEditDate: 2015-12-05T15:03:26.123Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>Is a custom field type and making use of <code>modifyElementsQuery</code> the only way to add my own <code>craft.entries</code> element criteria model attribute to modify the elements query?</p>
  
  <p>What if I do not want to add a new field to each entry type I want to use my  attribute with? The field would be a hidden pseudo field anyways, as I'm <strong>only working with other fields' data</strong>.</p>
  
  <p>My current solution is passing the element criteria model with all other params set to <code>craft()-&gt;elements-&gt;buildElementsQuery()</code> using a template variable. It works, yes, but I would really like to avoid this (pagination not possible and some other disadvantages).</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-10-13T16:48:47.780Z
Comments.Text: Have you thought about using a template hook? If you think that might be a viable solution, I can write up an official answer. :)
Comments.UserDisplayName: Lindsey D
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-10-13T18:18:45.387Z
Comments.Text: Thanks Lindsey, but I need this to be a bit more flexible than a template hook would make this for the user of the plugin.
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-10-13T18:21:28.913Z
Comments.Text: Makes sense... and that's exactly why I didn't spend time writing a full answer already. :)
Comments.UserDisplayName: Lindsey D
Tags: <plugin-development><elementcriteriamodel>
Title: Add custom element criteria attribute without a field type?
ThreadText: # Add custom element criteria attribute without a field type?
  
  (Asked by: username_0 on 2015-10-12)
  
  Is a custom field type and making use of `modifyElementsQuery` the only way to add my own `craft.entries` element criteria model attribute to modify the elements query?
  
  What if I do not want to add a new field to each entry type I want to use my attribute with? The field would be a hidden pseudo field anyways, as I'm **only working with other fields' data**.
  
  My current solution is passing the element criteria model with all other params set to `craft()->elements->buildElementsQuery()` using a template variable. It works, yes, but I would really like to avoid this (pagination not possible and some other disadvantages).
  
  **Tags:** plugin-development, elementcriteriamodel
  
  > **Comments:**
  > **username_1**: Have you thought about using a template hook? If you think that might be a viable solution, I can write up an official answer. :)  
  > **username_0**: Thanks username_1, but I need this to be a bit more flexible than a template hook would make this for the user of the plugin.  
  > **username_1**: Makes sense... and that's exactly why I didn't spend time writing a full answer already. :)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2015-10-13. Score: 5 | Accepted answer
  
  Currently, yes, the only way to modify an element query (when it’s not your own element type) is to do it with a custom field type, from its [modifyElementsQuery()](http://buildwithcraft.com/classreference/fieldtypes/IFieldType#modifyElementsQuery-detail) function.
  
  **UPDATE**
  
  As of Craft 2.5, plugins can listen for the [elements.onBeforeBuildElementsQuery](https://craftcms.com/docs/plugins/events-reference#elements-onBeforeBuildElementsQuery) and [elements.onBuildElementsQuery](https://craftcms.com/docs/plugins/events-reference#elements-onBuildElementsQuery) events to make changes to the query (or prevent it from getting executed).
  
  ```php
  craft()->on('elements.buildElementsQuery', function(Event $event) {
      /** @var ElementCriteriaModel $criteria */
      $criteria = $event->params['criteria'];
  
      /** @var DbCommand $query */
      $query = $event->params['query'];
  
      if ($criteria->myCustomParamName) {
          $query->andWhere( //...
      }
  });
  
  ```
  
  > **Comments:**
  > **username_3**: I'm trying to use your suggested `buildElementsQuery` event, but when I use `$criteria->myCustomParamName` it's not passing `myCustomParamName` to my plugin and I'm getting an error in the logfiles `Property "Craft\ElementCriteriaModel.myCustomParamName" is not defined.` I'm trying to use a custom parameter from the elementCriteria in my templates. What am I missing here?  
  > **username_2**: @Christiaan What version of Craft are you running?  
  > **username_3**: @username_2 I'm using Craft 2.5.2761  
  
  ---
  
  