Jump to content

Sorting Child Pages by field


dps123
 Share

Recommended Posts

Hi

I just need some advice on the best way to achieve this. The setup I have is:

Items

Item 1

Note 1

Note 2

Note 3

Item 2

Note 1

 

So I have note pages, which are children of item pages, which are within /items/. Each note has a 'created' datetime field on it. I also have a 'viewed_by' field which determines if a particular user has viewed that note. What I'm going to do is add the user's id to that field once they've viewed it.

On the webpage that the user sees, I need to display items that have one or more notes that have not been viewed by the current user, most recent first (based on 'created' field). After that, I need to show all other items in date order, most recent first.

Is there a way to sort the items like this using only ProcessWire $pages->get or $pages->find, or would I need to add the items to an array first and then loop through that? I thought I might have to loop through all the items to find ones that have an unread note, add those to an array, then loop through the remaining items and add those to the array.

Thanks!

 

Link to comment
Share on other sites

Not sure if you can achieve this all with one $pages->find operation.

You'd have to loop through the items and separate them into 2 page arrays (viewed yes and viewed no).

$items = $pages->find("template=item, sort=-created");

$notViewed = new PageArray();

foreach($items as $item) {

  if ($item->children("viewed_by!={$user}")->count()) {
    $notViewed->add($item);
    $items->remove($item);
  }

}

The $notViewed PageArray should hold all items where at least one note has not been viewed yet.

$items should only contain the items where notes have been viewed by the user since we removed the other ones inside the loop.

Now you can loop through $notViewed and $items to display them.

Link to comment
Share on other sites

And would I be able to use pagination with this option? I've read elsewhere people not being able to use pagination when using results directly from $pages>find.

Where did you read that?

The limit selector is specifically designed for $page->find calls so I would not know why it shouldn't work.

Your best bet is to give it a try and see what happens

$items = $pages->find("template=item, sort=-created, limit=x");
...
Link to comment
Share on other sites

And would I be able to use pagination with this option? I've read elsewhere people not being able to use pagination when using results directly from $pages>find.

I think maybe you mean not being able to use pagination when using results not directly from a single find query. And I believe that's true - if you build a PageArray in memory like $notViewed in gebeer's example then you can't use the pagination module on it out-of-the-box.

But there are solutions. Do a find query on your PageArray IDs like this:

$pages->find("id=$notViewed, limit=10, sort=-created");

Or check out Soma's techniques here.

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

×
×
  • Create New...