Answers.Body: <p>You know you don't add a new function to Crafts <code>EntryQuery</code> you create a custom <code>Element</code> type with a custom <code>Query</code> and add a function to that one. By the way you should probably fix your <code>namespaces</code></p>
  
  <pre><code>use by\prefix\elements\db\BirthdateQuery;
  use by\tariffsinfo\elements\BirthdateElement;
  </code></pre>
  
  <p>In order to use your function you'll have to do </p>
  
  <pre><code>BirthdateElement::find()-&gt;birthdate('whatever');
  </code></pre>
  
  <p>To search for a custom field in an entries <code>FieldLayout</code> you just need to call the parameter by handle... So when you include a field into the layout with the handle <code>birthday</code> you can do</p>
  
  <pre><code>{% set entries = craft.entries.birthday('whatever').all() %}
  </code></pre>
  
  <p>When you want to search for your custom element type in your frontend you'll have to include a function to Twig like Craft does</p>
  
  <pre><code>/**
   * Returns a new EntryQuery instance.
   *
   * @param mixed $criteria
   * @return EntryQuery
   */
  public function birthdays($criteria = null): BirthdateQuery
  {
      $query = BirthdateElement::find();
      if ($criteria) {
          Craft::configure($query, $criteria);
      }
  
      return $query;
  }
  </code></pre>
  
  <p>So you can access it via</p>
  
  <pre><code>{% set birthdates = craft.pluginName.birthdays.id(2).birthdate('whatever').all() %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-13T09:41:34.327Z
Answers.LastActivityDate: 2018-04-13T09:47:26.713Z
Answers.LastEditDate: 2018-04-13T09:47:26.713Z
Answers.UserDisplayName: Robin Schambach
Body: <p>I am developing a Craft 3 plugin and would need to add a new method like 
  <code>
  {% for item in craft.entries.section('sectionName').birthday('XYZ123').all() %}
  </code>
  According to doc <a href="https://docs.craftcms.com/v3/element-types.html#element-query-class" rel="nofollow noreferrer">https://docs.craftcms.com/v3/element-types.html#element-query-class</a>, I am using following code:</p>
  
  <p>File: <code>/by/prefix/element/Birthdate.php</code></p>
  
  <p><code>
      &lt;?php
      namespace by\prefix\elements;
      use craft\base\Element;
      use craft\elements\db\ElementQueryInterface;
      use by\prefix\elements\db\BirthdateQuery;
      class Birthdate extends Element
      {
          public $birthdate;
          public static function find(): ElementQueryInterface
          {<br>
              return new BirthdateQuery(static::class);
          }
      }
  </code></p>
  
  <p>File: <code>/by/prefix/element/db/BirthdateQuery.php</code></p>
  
  <p><code>
      &lt;?php
      namespace by\tariffsinfo\elements\db;
      use craft;
      use craft\db\Query;
      use craft\elements\db\ElementQuery;
      use craft\helpers\Db;
      use by\tariffsinfo\elements\BirthdateElement;
      class BirthdateQuery extends ElementQuery
      {
          public $birthdate;
          public function birthdate($value)
          {<br>
              return $value;
          }
      }
  </code></p>
  
  <p>and I am getting error message: 
  <code>Calling unknown method: craft\elements\db\EntryQuery::birthdate()</code></p>
  
  <p>Could anyone please help what am I doing wrong.</p>
  
  <p>Thanks</p>
  
Tags: <plugin-development><craft3>
Title: Craft 3 Plugin: Unable to add a new method to EntryQuery
ThreadText: # Craft 3 Plugin: Unable to add a new method to EntryQuery
  
  (Asked by: username_0 on 2018-04-13)
  
  I am developing a Craft 3 plugin and would need to add a new method like
  `{% for item in craft.entries.section('sectionName').birthday('XYZ123').all() %}`
  According to doc <https://docs.craftcms.com/v3/element-types.html#element-query-class>, I am using following code:
  
  File: `/by/prefix/element/Birthdate.php`
  
  `<?php
  namespace by\prefix\elements;
  use craft\base\Element;
  use craft\elements\db\ElementQueryInterface;
  use by\prefix\elements\db\BirthdateQuery;
  class Birthdate extends Element
  {
  public $birthdate;
  public static function find(): ElementQueryInterface
  {  
  return new BirthdateQuery(static::class);
  }
  }`
  
  File: `/by/prefix/element/db/BirthdateQuery.php`
  
  `<?php
  namespace by\tariffsinfo\elements\db;
  use craft;
  use craft\db\Query;
  use craft\elements\db\ElementQuery;
  use craft\helpers\Db;
  use by\tariffsinfo\elements\BirthdateElement;
  class BirthdateQuery extends ElementQuery
  {
  public $birthdate;
  public function birthdate($value)
  {  
  return $value;
  }
  }`
  
  and I am getting error message:
  `Calling unknown method: craft\elements\db\EntryQuery::birthdate()`
  
  Could anyone please help what am I doing wrong.
  
  Thanks
  
  **Tags:** plugin-development, craft3
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-04-13. Score: 1
  
  You know you don't add a new function to Crafts `EntryQuery` you create a custom `Element` type with a custom `Query` and add a function to that one. By the way you should probably fix your `namespaces`
  
  ```
  use by\prefix\elements\db\BirthdateQuery;
  use by\tariffsinfo\elements\BirthdateElement;
  
  ```
  
  In order to use your function you'll have to do
  
  ```
  BirthdateElement::find()->birthdate('whatever');
  
  ```
  
  To search for a custom field in an entries `FieldLayout` you just need to call the parameter by handle... So when you include a field into the layout with the handle `birthday` you can do
  
  ```
  {% set entries = craft.entries.birthday('whatever').all() %}
  
  ```
  
  When you want to search for your custom element type in your frontend you'll have to include a function to Twig like Craft does
  
  ```
  /**
   * Returns a new EntryQuery instance.
   *
   * @param mixed $criteria
   * @return EntryQuery
   */
  public function birthdays($criteria = null): BirthdateQuery
  {
      $query = BirthdateElement::find();
      if ($criteria) {
          Craft::configure($query, $criteria);
      }
  
      return $query;
  }
  
  ```
  
  So you can access it via
  
  ```
  {% set birthdates = craft.pluginName.birthdays.id(2).birthdate('whatever').all() %}
  
  ```
  
  
  ---
  
  