I have a list of posts posted on different dates

I can order the posts by date

$loop_args = array(
    'post_type' => 'post',
    'order_by' => 'date',
    'order' => 'ASC'    

How can I order the post by comment date if the post have a comment.

So sort the posts by activity

1 Answer 1


I like the idea proposed in this comment. Connecting the posts table to the comments table seems like it'd be some super complicated SQL (well all SQL is complicated to me). Instead the link proposes that you add a post meta field to the post to hold the date of the most recent comment.

add_action('comment_unapproved_to_approved', 'wpa_144482_comment_approved');

function wpa_144482_comment_approved($comment) {
    $comment_post_ID = $comment->comment_post_ID;
    $date = $comment->comment_date;
    update_post_meta( $comment_post_ID, '_recent_comment_date', $date );

Then you should be able to sort your posts by the _recent_comment_date field.

There is also the Filter by Comments plugin but it hasn't been updated in a long while so I couldn't vouch for it.

  • Do you mean "sort your posts by the _recent_comment_date field"? May 15, 2014 at 11:40
  • Did you test the plugin with the latest version. It hasn't been updated since wordpress 3.1.4, so it might not work May 15, 2014 at 11:40
  • I wouldn't bother with the plugin. The first suggestion is the same as I would've done. May 15, 2014 at 11:41
  • @TheDeadMedic yes, I did mean that. Copy/paste fail! May 15, 2014 at 11:43
  • 1
    @PieterGoosen no I haven't tested it. Have updated my answer to include that caveat. May 15, 2014 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.