<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>

CC BY-SA 3.0
2016-02-08T16:42:56.590Z
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?
ctigelaar
CC BY-SA 3.0
2016-02-08T21:51:38.937Z
@Christiaan What version of Craft are you running?
Brandon Kelly
CC BY-SA 3.0
2016-02-09T06:39:55.007Z
@Brandon I'm using Craft 2.5.2761
ctigelaar
CC BY-SA 3.0
2015-10-13T11:30:37.477Z
2015-12-05T15:03:26.123Z
2015-12-05T15:03:26.123Z
Brandon Kelly
<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>

CC BY-SA 3.0
2015-10-13T16:48:47.780Z
Have you thought about using a template hook? If you think that might be a viable solution, I can write up an official answer. :)
Lindsey D
CC BY-SA 3.0
2015-10-13T18:18:45.387Z
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.
carlcs
CC BY-SA 3.0
2015-10-13T18:21:28.913Z
Makes sense... and that's exactly why I didn't spend time writing a full answer already. :)
Lindsey D
<plugin-development><elementcriteriamodel>
Add custom element criteria attribute without a field type?
# 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 Lindsey, 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() function.
UPDATE
As of Craft 2.5, plugins can listen for the elements.onBeforeBuildElementsQuery and elements.onBuildElementsQuery events to make changes to the query (or prevent it from getting executed).
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**: @Brandon I'm using Craft 2.5.2761  

---

