Jump to content

Sort by children's create date


suntrop
 Share

Recommended Posts

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

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 by dragan
correction: field created instead of modified
Link to comment
Share on other sites

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

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

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...