TransWikia.com

Ordering entries by custom field (integer)

Craft CMS Asked by klaxon on December 27, 2020

I’m new to craft so may it be a basic question, however I can’t find an answer for it :-/

I have custom field ‘width’ in my entry type defined as integer.
What I need to achieve is quite simple:

{% set entries = craft.entries.section('windows').find({'width':'> 0'}).order(width) %}

The important now is the .order part it’s not working right now :-(. What is correct way to do this? I have found some plugins but I believe craft can do this somehow… It always does (so far).

Thanks for any info on this.

2 Answers

Craft 2

Either of these should work (they are equivalent):

{% set entries = craft.entries.width('> 0').section('windows').order('width') %}

or

{% set entries = craft.entries({
  section: 'windows',
  order: 'width',
  width: '> 0'
}) %}

The reason you are getting "Array to string conversion" error is that find has turned your ElementCriteriaModel into an array.

Craft 3

order is now orderBy

Craft 3 Docs: Element Queries

Correct answer by Marion Newlevant on December 27, 2020

Have you tried putting in 'width' instead of width?

The order parameter expects a string, not an object, as it just needs to know the name of the field name to filter by.

Final code would look like:

{% set entries = craft.entries.section('windows').find({'width':'> 0'}).order('width') %}

Answered by Patrick Harrington on December 27, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP