Skip to content

Instantly share code, notes, and snippets.

@Anubarak
Last active April 4, 2019 16:26
Show Gist options
  • Save Anubarak/f610c1a1234b4f014046a7919ddbd142 to your computer and use it in GitHub Desktop.
Save Anubarak/f610c1a1234b4f014046a7919ddbd142 to your computer and use it in GitHub Desktop.
<?php
// Element API endpoint
'news.json' => function() {
return [
'elementType' => Entry::class,
'criteria' => [
'section' => 'news'
'join' => [
[
'LEFT JOIN',
'{{%relations}} as myrelations',
'[[myrelations.sourceId]] = [[elements.id]] AND [[myrelations.fieldId]] = 9'
],
[
'LEFT JOIN',
'{{%content}} as mycontent',
'[[mycontent.elementId]] = [[myrelations.targetId]]'
]
],
'orderBy' => 'mycontent.title desc'
],
];
},
// TWIG
{% set fieldId = 9 %}
{% set entries = craft
.entries
.section('awesomeNews')
.leftJoin('{{%relations}} as myrelations', '[[myrelations.sourceId]] = [[elements.id]] and [[myrelations.fieldId]] = ' ~ fieldId)
.leftJoin('{{%content}} as mycontent', '[[myrelations.targetId]] = [[mycontent.elementId]]')
.groupBy('elements.id')
.orderBy({'mycontent.title': SORT_ASC})
.all()
%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment