Siyah
Siyah

Reputation: 2897

Modifying code to get recent commented posts above older post

I am trying to get this work, but it's not entirely working. This is how I currently query my posts:

<?php 
    // the query
    $the_query = new WP_Query(  array( 'posts_per_page' => -1 ) ); 
    if ( $the_query->have_posts() ) : 
    ?>
        <!-- pagination here -->
        <!-- the loop -->
        <?php 
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
        ?>                          
            <li data-href="<?php $zlink = get_the_permalink(); echo preg_replace("#/$#im", '', $zlink);?>">
                <div>
                    <a class="button" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </div>

What I am trying to achieve, is the following:

  1. Show recently commented post on top of posts that have a newer publish date...
  2. If a blog-post has a comment, regardless of how old that blog-post is, I want that blog-post above a newer blog-post, if that newer one has no update (meaning: no recent comments).

I started by showing the recently commented posts on top (see below), but this totally neglects the fact that there are posts without comments and I can't find a way to combine both and show them in one list.

<?php
    $args = array(
    'status' => 'approve',
    'number' => 6,
    'order' => 'DESC'
);
$comments = get_comments($args);

foreach($comments as $comment) : $count++;

        $post_args = array(
            'post_type' => 'post',
            'p' => $comment->comment_post_ID,
            'posts_per_page' => 1
            );

        $posts = get_posts($post_args);

        foreach($posts as $post) : setup_postdata($post);

            the_title();
        endforeach;

endforeach;
?>

Could someone help?

Upvotes: 0

Views: 70

Answers (1)

pendo
pendo

Reputation: 842

The below creates an empty array, adds all the posts with comments to that array. It then adds all the posts without comments. Finally, it sorts them by the comment date or the post date, depending on whether that post had any comments or not

//create an array to stuff all your posts in
$arrAllPosts = array();

$args = array(
    'post_type'         => 'post',  //show only posts (not pages, etc)
    'comment_count'     => array(   //pass it an array
        'value'     => 1,           //value is 1, with compare means greater than or equal to 1
        'compare'   => '>='
    ),
    'posts_per_page'    => -1       //gimme all of them
);
$postsWithComments = new WP_Query($args);


while($postsWithComments->have_posts()) {$postsWithComments->the_post();
    //get the comments for this post
    $comments = get_comments(array(
        'post_id'   => get_the_ID(),    //pass the post ID
        'orderby'   => 'comment_date',  //tell it to sort by the comment date, so you only get the latest
        'number'    => 1                //just get the latest
    ));
    foreach($comments as $comment) { //we're only looping this once, since there is only one
        $arrAllPosts[] = array(
            'dateToSortBy'  => $comment->comment_date,  //we'll use comment date to sort by later, instead of the post date
            'the_post_obj'  => $post                    //add the global post object, which is currently set to the current post
        );
    }
}
//now we get the posts with no comments
$args = array(
    'post_type'         => 'post',  //Only posts (not pages, etc)
    'comment_count'     => 0,       //Posts with no comments only
    'posts_per_page'    => -1       //gimme all of them
);

$postsNoComments = new WP_Query($args); //run it

while($postsNoComments->have_posts()) {$postsNoComments->the_post();    //loop it
    $arrAllPosts[] = array(
        'dateToSortBy'  => $post->post_date,  //we'll use the post date to sort by
        'the_post_obj'  => $post            //add the global post object, which is currently set to the current post
    );

}

function date_compare($a, $b) { //create a custom function to sort the array by the date of the post or the date of the comment
    $tmA = strtotime($a['dateToSortBy']);   //convert to time
    $tmB = strtotime($b['dateToSortBy']);   //convert to time
    return ($tmA < $tmB)?true:false;
}
usort($arrAllPosts, 'date_compare');

//Display the title for each post, from the sorted list
foreach($arrAllPosts as $curPost) { 
    $post = $curPost['the_post_obj'];   //make this the global post object
    setup_postdata($post);              //setup the data
    echo "<a href='" . get_the_permalink() . "'>" . get_the_title() . "</a>";
} 

Upvotes: 1

Related Questions