4

With element queries, we can search for elements using search method:

craft.entries.search('someField:something')

or by using field handle as method:

craft.entries.someField('something')

How these two techniques are different from each other and when whould we use each?

6

The search parameter gives you an entire range of search functionality, as explained in the docs at https://craftcms.com/docs/3.x/searching.html

Using a field handle as a parameter essentially specifies what the value of the field must equates to. How this is determined can vary, based on the field type, but in most cases it will be an exact match.

So in the following example, only entries with the exact plaintext field value something will be returned.

craft.entries.somePlaintextField('something')

If you dump the query's raw SQL then you will see this.

{{ dump(craft.entries.somePlaintextField('something').getRawSql()) }}

Outputs: SELECT ... WHERE ... AND (`content`.`field_somePlaintextField`='something') ...

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.