Jump to content

Sort parents by date field in children?


MarcC
 Share

Recommended Posts

On the front end of a website I'm working on, I would like to sort some pages ("Project") by the nearest due date of their children. Here's an example:

Before:

Project 1

    Assignment -- Due Date 20 January 2012

    Assignment -- Due Date 21 January 2012

Project 2

    Assignment -- Due Date 18 January 2012

    Assignment -- Due Date 19 January 2012

After:

Project 2

    Assignment -- Due Date 18 January 2012

    Assignment -- Due Date 19 January 2012

Project 1

    Assignment -- Due Date 20 January 2012

    Assignment -- Due Date 21 January 2012

Any tips on a good way to do this? Thanks. I can already sort the children fine, but the parents I dunno.

Link to comment
Share on other sites

I don't see an easy option to do that, but with a little module you could on each save of a child page update a date field on the parent. Then sort it by that field. Let us know so we can help you get started.

  • Like 3
Link to comment
Share on other sites

Edit well if there not too many projects you could try and evaluate the dates and store them on runtime to sort by that value afterwards when you loop them out.

$pa = $pages->find("template=project");
if(count($pa)){
    foreach($pa as $p){
        if(!$p->numChildren) continue; // no assignments
        // store a temporary value to the project page
        $p->tmp_date = $p->children("sort=-due_date")->first()->due_date;
    }
    // output the projects
    foreach($pa->sort("-tmp_date") as $p){
        echo "<li>$p->title</li>";     
        if(!$p->numChildren) continue;
        foreach($p->children("sort=-due_date") as $a){
            // output assignments
        }        
    }
}

This will work well if you don't have thousands of pages. The module approach would be the more efficient and scaleable.

  • Like 4
Link to comment
Share on other sites

  • 3 months later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...