suntrop Posted December 5, 2018 Share Posted December 5, 2018 Hi there! I'd like to sort some pages – kind of categorised blog posts. CMS: Blog - GROUP #1 - Cat A - Post 2018-12-01 - Post 2018-10-10 - Cat B - Post 2018-11-11 - Post 2018-05-05 - GROUP #2 - Cat C - Post 2018-12-05 - Post 2018-01-01 - GROUP … The user can select from different options to sort GROUP #1, GROUP #2, GROUP #n pages by date, title and … latest posts. If the user selects "Latest posts" it should show as: See latest posts from … » GROUP #2 (with a Post from 2018-12-05) » GROUP #1 (with a Post from 2018-12-01) » … Is this possible with a selector/PW's API? I guess it doesn't work like that, but perhaps I can utilise the newly added option to create a custom WireArray (https://processwire.com/blog/posts/processwire-3.0.117-core-updates/)? I think I have to loop the groups, find the latest post and create a new property with the timestamp. Link to comment Share on other sites More sharing options...
dragan Posted December 5, 2018 Share Posted December 5, 2018 (edited) You can just do something like $posts = $pages->find("has_parent=123, template=blogpost, sort=-created, limit=5"); 123 = id of your blog main parent page limit = number of groups (I'm guessing you want just one per group) In your foreach, it's easy to output the blog post's group #, perhaps with parentsUntil or similar... Edited December 5, 2018 by dragan correction: field created instead of modified Link to comment Share on other sites More sharing options...
suntrop Posted December 5, 2018 Author Share Posted December 5, 2018 This makes $posts only contain the posts. But I need a list of the groups. The webpage looks something like this at the end: See latest posts from … » GROUP #2 (with a Post from 2018-12-05) » GROUP #1 (with a Post from 2018-12-01) » GROUP #x (with …) » GROUP #y (with …) … Link to comment Share on other sites More sharing options...
szabesz Posted December 5, 2018 Share Posted December 5, 2018 Hi, regarding creating groups, we have various discussions in the forum, for example: https://processwire.com/talk/topic/18775-fun-with-hooks-pagearraygroupby/ https://processwire.com/talk/topic/14129-list-pages-by-year-similar-to-blog-archive/ https://processwire.com/talk/topic/15369-group-pages-by-field/?tab=comments#comment-137442 Link to comment Share on other sites More sharing options...
OLSA Posted December 9, 2018 Share Posted December 9, 2018 On 12/5/2018 at 7:32 PM, suntrop said: I think I have to loop the groups, find the latest post and create a new property with the timestamp. Yes. Here is part how to get that, but you need to finish it (write your selectors, change "date" with your date field). Not good as direct sql query, but it could work, need to test. // get your groups $groups = $pages->find('your selector(s) to get groups'); // prepare empty pages array ("container") $posts = new PageArray(); foreach ($groups as $group){ // !!! bad thing is to do queries inside foreach // but good thing is to use "findOne" $p = $pages->findOne("has_parent=$group, template=post, sort=-date"); if($p->id){ $posts->add($p); } } // sort posts by date $posts->sort('-date'); // render links $out = '<ul>'; foreach($posts as $post){ $out.= '<li> <a href="'.$post->parent->parent->url.'">'.$post->parent->parent->title.'</a> <a href="'.$post->url.'"> ('. $post->title .' - '. $post->date .')</a> </li>'; } $out.= '</ul>'; echo $out; Also, here option to use instead $pages->findOne('has_parent=.................') this: $group->findOne('template=post, sort=-date'). Regards. Link to comment Share on other sites More sharing options...
suntrop Posted December 10, 2018 Author Share Posted December 10, 2018 Hi OLSA, thanks for your code. On the second look, I think this can work for me. But this one focuses on the posts again, not the groups. I think I found a fairly simple approach, but haven't tested thoroughly $i = 0; foreach ($user->groups() as $group) { $post = $group->find('template=posting, sort=-created, limit=1')->first(); $user->groups()->eq($i)->data('latest', $post->created); $i++; } // later I can foreach($user->groups()->sort('-latest') as $group) {…} I am just adding a new property 'latest' with the timestamp and can sort my groups by this new property later where I output my groups. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now