I have two channels books and authors
I need to list all books sorted alphabetically by author. I have this working with the following code:
<ul>
{#
// loop through authors so we can sort books
// by author last name
#}
{% for author in craft.entries.section('authors').order('lastName').all() %}
{# find all books related to the author #}
<ul>
{% for entry in craft.entries.section('books').relatedTo(author).all() %}
<li>{{ entry.title }}</li>
{% endfor %}{# end books for each author #}
</ul>
{% endfor %}{# end authors #}
</ul>
This works fine, but I"m not sure how to paginate this. I want to paginate the books which is the nested loop and will likely only contain 2-4 books per author.
If I paginate the authors loop I'll have 5 authors per page but each author will have a different number of books so each page will have different number of books. Not what I want.
If I paginate the book loop it's not going to see all the books only the ones from the author of the current author loop.
However the total number of books is 100+. How do I paginate the total number of book results.
--------------
If it were possible to do the alpha sort based on the related field without nesting for loops that would be ideal? Is this possible?
------------- UPDATE ----------------
add a new field to the books channel where the authors last name is manually added and now using .order('authorLastName')
which works but it clunky becaue they have to select the author from the authors channel and add in their last name in a second field.