To get the latest comment use get_comments()
.
get_comment_date
returns the date of a comment in any format for PHP’s date()
.
Now it is easy. Let’s put the logic into a function to keep the global namespace clean:
/**
* Returns the number of days since the latest comment.
*
* @return int
*/
function get_days_since_last_comment( $post_id = 0 )
{
$args = array (
'number' => 1,
'status' => 'approve'
);
0 !== $post_id and $args['post_id'] = (int) $post_id;
// Array of comment objects.
$latest_comment = get_comments( $args );
// No comments found.
if ( ! $latest_comment )
{
return -1;
}
$comment_unix = get_comment_date( 'U', $latest_comment[0]->comment_ID );
return round( ( time() - $comment_unix ) / 86400 );
}
Add the function to your plugin or to your theme’s functions.php
.
To display a special message:
if ( get_days_since_last_comment() > 7 )
{
print 'Looks like everything has been said.';
}
To get the days for a specific post (here ID 123
) :
if ( get_days_since_last_comment( 123 ) > 7 )
{
print 'Looks like everything has been said.';
}