Skip to content

[5.x]: orderBy on custom field with advance queries #16725

@anita-chouhan

Description

@anita-chouhan

What happened?

Description

I am trying to use the orderBy clause on a custom date field for index page entries. The date fields are empty for some entries, and I would like those entries to be placed at the end.

The desired outcome can be achieved by using the fieldValueSql, but this requires an entryType. Is there an alternative method to accomplish the same without an entryType?

Current query:

{% do entries.orderBy('ISNULL(' 
      ~ fieldValueSql(entryType('notes'), 'datePublished') 
      ~ '), datePublished ASC, title ASC') %}

Craft CMS version

5.5.9

PHP version

8.2.20

Database type and version

MySQL 8.0.36

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions