Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display the most recent comments' date -Outside- of single.php? WordPress

  • SOLVED

I would like to show the date from the most recent comment posted as that post's updated date. This information will be used on index and category pages.

In the example below, I would like to replace the "????????" with the date of the most recent comment for that post.

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="postname">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><?php comments_popup_link('No Reviews', '1 Review', '% Reviews'); ?></p>
</div>
<em class="latestreviewdate">????????</em>
</li>

Answers (4)

2010-09-10

Jonah Schulte answers:

This should do the trick:


<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="postname">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><?php comments_popup_link('No Reviews', '1 Review', '% Reviews'); ?></p>
</div>

<?php
$comment_date = $wpdb->get_results('SELECT DATE_FORMAT(comment_date, "%W, %M %D, %Y") as comment_date FROM wp_comments WHERE comment_post_ID = '.get_the_ID().' ORDER BY comment_ID DESC LIMIT 1');
?>

<em class="latestreviewdate">
<?php
if (isset($comment_date[0]->comment_date))
{
print $comment_date[0]->comment_date;
}
else
{
the_time('l, F jS, Y');
}
?>
</em>



Let me know if you have any trouble with it!

-Jonah


badnews comments:

Hey this seems to works great.

Is it possible to get the date in 30/06/2010 format?

Thanks


Jonah Schulte comments:

Here's how you can format the date the way you requested:


<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="postname">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><?php comments_popup_link('No Reviews', '1 Review', '% Reviews'); ?></p>
</div>

<?php
$comment_date = $wpdb->get_results('SELECT DATE_FORMAT(comment_date, "%d/%m/%Y") as comment_date FROM wp_comments WHERE comment_post_ID = '.get_the_ID().' AND comment_approved=1 ORDER BY comment_ID DESC LIMIT 1');
?>

<em class="latestreviewdate">
<?php
if (isset($comment_date[0]->comment_date))
{
print $comment_date[0]->comment_date;
}
else
{
the_time('d/m/Y');
}
?>



I'd like to point out that everyone else who answered your question pretty much just copied my solution, except that mine also defaults back to the original posting date if there aren't any comments on the post yet.

Cheers,
Jonah


badnews comments:

This is exactly what I needed appreciate your help very much.

Thanks!

2010-09-11

Pau answers:

this is my short version of getting the date of recent comment using built-in wordpress functions.

<em class="latestreviewdate">
<?php
$comments = get_comments( 'post_id=' . $post->ID );
echo get_comment_date('d/m/Y', $comments[0]->comment_ID);
?></em>

2010-09-10

enodekciw answers:

I got some function for you.
Paste this code into your functions.php file:
function latest_comment_date($postid) {
global $wpdb;
$result = $wpdb->get_results("SELECT DATE_FORMAT(comment_date, \"%d/%m/%Y\") as comment_date FROM $wpdb->comments WHERE comment_post_ID=$postid AND comment_approved=1 ORDER BY comment_date DESC LIMIT 1");
echo $result[0]->comment_date;
}


Now, when you want to grab latest comment date, just do something like this:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="postname">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p><?php comments_popup_link('No Reviews', '1 Review', '% Reviews'); ?></p>
</div>
<em class="latestreviewdate"><?php latest_comment_date($post->ID); ?>
</li>


enodekciw comments:

ooops, forgot to order results ;)
now it should be working as it is supposed to.


enodekciw comments:

done some more tweaking:
function latest_comment_date($postid, $text, $no_comments_text) {
global $wpdb;
$result = $wpdb->get_results("SELECT DATE_FORMAT(comment_date, \"%d/%m/%Y\") as comment_date FROM $wpdb->comments WHERE comment_post_ID=$postid AND comment_approved=1 ORDER BY comment_date DESC LIMIT 1");
if($result) {
echo $text . $result[0]->comment_date;
} else {
echo $no_comments_text;
}
}


now use latest_comment_date() function like this:

<?php latest_comment_date($post->ID, 'Text which appears before date (optional): ', 'Text which appears if there are no comments yet'); ?>

2010-09-10

Nilesh shiragave answers:

Hi

Add this function inside your theme's function.php file


function get_latest_comment_date($id)
{

global $wpdb;
$comment_date = $wpdb->get_results('SELECT DATE_FORMAT(comment_date, "%W, %M %D, %Y") as comment_date FROM wp_comments WHERE comment_post_ID = '.$id.' ORDER BY comment_ID DESC LIMIT 1');
if (isset($comment_date[0]->comment_date))
{
$date=$comment_date[0]->comment_date;
return date('d/m/Y',strtotime($date));
}
else
{
return 'No Comments';//Replace no comments with other text which you want
}
}




And add this code inside loop where you want show the last viewed date


<em class="latestreviewdate">
<?php
echo get_latest_comment_date($post->ID);
?>
</em>