I have entries from a section 'title' which all have an associated category 'author'. Category has an associated field 'sortName'. Essentially, I am trying to sort a bunch of books by author last name (stored in sortName).
I am trying to sort all entries in 'title' by 'author.sortName' but as they are in separate tables, I cannot make the relation without throwing an error.
Is there a way to query all entries, sort them by a category field and then loop through them?
I have everything working except the grouping which is NOT actually sorting the entries by the titleAuthor Category at all. Ideally I want it to sort by titleAuthor.sortName
Right Now I'm trying:
{% if categorySlug is defined %}
{% set category = craft.categories.slug(categorySlug).one() %}
{% set entries = craft.entries.section('titles')
.relatedTo(category)
.titleDisplayNYLA(true)
.orderBy('titlePublicationDate DESC')
.all()
%}
{% set title = category.title %}
{% set entriesGrouped = entries|group('titleAuthor.first().title') %}
{% else %}
{% set entries = craft.entries()
.section('titles')
.titleFeatureHome(':notempty:')
.titleDisplayNYLA(true)
.all()
%}
{% set title = 'NYLA•Tites' %}
{% set info = craft.entries.section('titlesIndex').one() %}
{% set entriesGrouped = entries |group('titleAuthor.title') %}
{% endif %}
{% for group in entriesGrouped %}
{% for category, entry in group %}
{{entry.title}}
{% for category in entry.titleAuthor.all() %}{{ category.title }} {% endfor %}
{% endfor %}
{% endfor %}