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.



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


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() %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.