Jump to content
johnstephens

How to sort PageArray by a field of first linked page in page reference field

Recommended Posts

I have a list of articles that I want to sort alphabetically by the author's lastname. The author(s) are linked to each article's page using a page reference field, and the author pages have a field for their lastname. Most articles have only one linked author page, but some may have more than one. For sorting, I only want to consider the first author in the list.

I'm stumped how to make this work.

$articles = page()->children('sort=FIRSTAUTHOR'); // ??

If I get one page from the list, I would be able to select the field I want to sort by like so:
 

foreach ($articles as $article) {
    $sortfield = $article->authors->first()->lastname;
}

Thanks in advance for any guidance you can offer!

Share this post


Link to post
Share on other sites

I figured it out! Sub-selectors was the wrong approach, but a subfield selector seems to do the trick:

$articles = page()->children('sort=authors.lastname');

Woo. Love it!

  • Like 2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...