Sort posts by newest child while keeping hierarchy intact

I have the following hierarchy within my wordpress posts (e.g.):

-> Post 1 (pubdate: 2011-01-01)
 --> Post 5 (pubdate: 2011-01-02)
  ---> Post 6 (pubdate: 2011-01-03)
   ----> Post 7 (pubdate: 2011-01-04)

-> Post 2 (pubdate: 2011-03-01)
 --> Post 8 (pubdate: 2011-03-02)

-> Post 3 (pubdate: 2011-02-01)
 --> Post 9 (pubdate: 2011-02-02)
  ---> Post 10 (pubdate: 2011-02-03)

-> Post 4 (pubdate: 2011-04-01)

What I now would like to achieve is to sort the parent posts by the publish date of the newest respective child post. The important thing is that the hierarchy should stay intact. This would lead to the following order in the example (from newest to oldest):

Read More
-> Post 4 (pubdate: 2011-04-01)

-> Post 2 (pubdate: 2011-03-01)
 --> Post 8 (pubdate: 2011-03-02)

-> Post 3 (pubdate: 2011-02-01)
 --> Post 9 (pubdate: 2011-02-02)
  ---> Post 10 (pubdate: 2011-02-03)

-> Post 1 (pubdate: 2011-01-01)
 --> Post 5 (pubdate: 2011-01-02)
  ---> Post 6 (pubdate: 2011-01-03)
   ----> Post 7 (pubdate: 2011-01-04)

Any idea how I can get this working?
Thanks!

EDIT: I added the following part based on the question of Rarst on how i retrieve the structure.

I retrieve the posts with the following query:

$args = array(
  'cat' => 7,
  'post_type' => 'articles', 
  'orderby' => 'post_date', 
  'posts_per_page' => -1, 
  'order' => 'DESC' 
); 
$records = get_posts($args);
foreach($records as $post) {
  get_template_part( 'index', 'article' );
}

I then use this function to check how many parents an article has…

// count amount of parents of a post
function usr_count_children($cid) {
  $childrenArray = count(get_pages("post_type=articles&child_of=".$cid));
  return $childrenArray;
}

…and add the returned number as css class to the html element of the article (which is then styled based on that).

EDIT 2:

Ok, that’s what I’ve got so far:

    $parentPosts = get_posts(array(
    'cat' => 7, 
    'post_type' => 'articles',
    'post_status' => 'publish',
    'post_parent' => 0, 
    'posts_per_page' => -1,
    'orderby' => 'post_date',
    'order' => 'DESC' ));

foreach ($parentPosts as $post){

    echo "+ ".$post->post_title." ID: ".$post->ID."<br/>";

    // Look up newest child (query, sort by date, limit to one)
    $newestChild = get_pages(array(
        'child_of' => $post->ID,
        'post_type' => 'articles',
        'post_status' => 'publish',
        'sort_column' => 'post_date',
        'sort_order' => 'DESC',
        'number' => 1 ));

    foreach($newestChild as $child){
        echo "– ".$child->post_title." ID: ".$child->ID."<br/>";
    }
}

I’m stuck with the second query (get newest child), it wont return any children unless I remove 'number' => 1. I really tried a few things (like using WP_Query instead, I even tried raw SQL queries) but I can’t get my head around it. Anyone?

Related posts

Leave a Reply

1 comment

  1. How do you retrieve and echo this structure in first place?

    I don’t think you can query by something like this (without some wicked SQL), so it would be probably easier to:

    1. Retrieve parent posts.
    2. Look up newest child (query, sort by date, limit to one).
    3. Reorder parent posts accordingly.