• I’ve looked everywhere, but I can’t seem to be able to find the solution to this.

    I want to display the date the post was last commented on. I am creating a user review site and would like to display the date of the last comment(review) on the homepage as something like ‘Last reviewed on [date]’ .

    How can I do this? Any help or direction would be much appreciated. Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • You’ll want something like (this isn’t tested)

    <?php
    $PostID = get_the_ID();
    $comments = get_comments('post_id=$PostID&number=1');
    foreach($comments as $comment) :
    	print "Last reviewed on". $comment->date. ".";
    endforeach;
    ?>

    I don’t have time to test if it works but if it doesn’t reference get_comments

    Thread Starter Joyful Thiek

    (@jothiek)

    Thanks for this. It almost works.

    The date was not displayed with your code, so I changed it to:

    print "Last reviewed on". $comment->comment_date. ".";

    That shows the date. But I’m faced with another problem. I actually want to display the corresponding date with each post in the home page (index.php), but all posts are showing the same date (date of latest comment).

    What I want to achieve is – I want each individual post to show its own date of last comment. I’m totally lost!

    Oh yeah, wrote it really quickly so bound to be errors!

    You want to change get_the_ID(); with I think it is $post->ID; then I think for what you want.

    Thread Starter Joyful Thiek

    (@jothiek)

    I’m officially freaked out. I can’t get it to work. It’s showing the same results. ??

    The ID is right, so I think. I inserted this line of code:

    echo $PostID;

    It shows different IDs for different posts. So that means the IDs are good, right? But the date is still the same for all the posts. I typed in new comments, but to no use. It shows the same date and time for all posts.

    I wonder if it has something to do with the version of WordPress I’m using. I’m on 2.9.1 on my local WAMP. I accidentally selected 3.0 for this topic.

    Okay I’ve looked into it and I reckon now the best way is to use a custom query, perhaps there are other ways. This code works for me (tried and tested) so hopefully will work similarly for you.

    $PostID =  get_the_ID();
    		$GetCommentDateByPostID = mysql_fetch_array(mysql_query("SELECT comment_date FROM wp_comments WHERE comment_post_ID = $PostID ORDER BY comment_date DESC LIMIT 1"));
    		echo $GetCommentDateByPostID['comment_date'];

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to display the date of the last comment’ is closed to new replies.