Jump to content

Sorting results then grouping


Jon
 Share

Recommended Posts

Hello I am looking for abit advice on how to achieve this. I have a number of pages all containing a date. I would like to display the results sorted by dates but with a header for each month so for example.

April

Date 1

Date 2

Date 3

May

Date 1

Date 2

Etc

Does process wire have any built in function in the API for this? Or will it need to be done via a database query

Cheers

Jon

Link to comment
Share on other sites

Hello guys,

Thanks for the info guys, Macrura that post was very useful and managed to adapt it to meet the my needs.

With regards to the wire('page') find is that specifically for getting repeater fields?

I like the way you can add properties aswell that's cool!

Cheers once again guys :)

Link to comment
Share on other sites

@Jon - the wire class can be used inside functions - so in the case of Joss' issue, he wanted the solution to be in a function. when outside of a function like in a template you can use the $pages variable directly...

  • Like 1
Link to comment
Share on other sites

  • 3 months later...

How about sort pages by date (in the main query), and echo month name if it's different from the previous?

Just used this excellent suggestion and thought I might as well post some code here. :)

$upcoming_events = $pages->find("template=event, event_date>=today, sort=event_date");
$prev_event_month = "";
foreach($upcoming_events as $upcoming_event) {
    $month = date("F", $upcoming_event->getUnformatted("event_date"));
    if($month !== $prev_event_month) {
        echo "<h2>{$month}</h2>";
    }
    echo "<h3>{$upcoming_event->title}</h3>"; // more event markup here
    $prev_event_month = $month;
}
  • Like 1
Link to comment
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
 Share

  • Recently Browsing   0 members

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