Index: wp-includes/comment.php
===================================================================
--- wp-includes/comment.php	(revision 16595)
+++ wp-includes/comment.php	(working copy)
@@ -402,25 +402,101 @@
  * @return string Last comment modified date.
  */
 function get_lastcommentmodified($timezone = 'server') {
-	global $cache_lastcommentmodified, $wpdb;
-
+	global $cache_lastcommentmodified, $wpdb, $wp, $wp_query;
+	
 	if ( isset($cache_lastcommentmodified[$timezone]) )
 		return $cache_lastcommentmodified[$timezone];
 
+	//If wp_query has not been completely loaded (e.g when it's called in send_headers()),
+	//this has to parse the query, but without fetching anything from the database.
+	if( empty($wp_query->query_vars) ) {
+		$wp->build_query_string();
+		$wp_query->parse_query($wp->query_vars);
+	}
+	
+	$q = $wp_query->query_vars;
+
 	$add_seconds_server = date('Z');
 
-	switch ( strtolower($timezone)) {
+	switch ( strtolower($timezone) ) {
 		case 'gmt':
-			$lastcommentmodified = $wpdb->get_var("SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
+			$select = 'SELECT comment_date_gmt';
 			break;
 		case 'blog':
-			$lastcommentmodified = $wpdb->get_var("SELECT comment_date FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1");
+			$select = 'SELECT comment_date';
 			break;
 		case 'server':
-			$lastcommentmodified = $wpdb->get_var($wpdb->prepare("SELECT DATE_ADD(comment_date_gmt, INTERVAL %s SECOND) FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 1", $add_seconds_server));
+		default:
+			$select = $wpdb->prepare('SELECT DATE_ADD(comment_date_gmt, INTERVAL %s SECOND)', $add_seconds_server);
 			break;
 	}
 
+	$join = '';
+	$needposts = false;
+	$where = "WHERE comment_approved = '1'";
+
+	$p = '';
+	if ( '' !== $q['p'] )
+		$p = $q['p'];
+
+	elseif ( '' !== $q['page_id'] )
+		$p = $q['page_id'];
+
+	elseif ( '' !== $q['attachment_id'] )
+		$p = $q['attachment_id'];
+
+
+	$name = '';
+	if ( '' !== $q['name'] )
+		$name = $q['name'];
+
+	elseif ( '' !== $q['pagename'] )
+		$name = $q['pagename'];
+
+	elseif ( '' !== $q['attachment'] )
+		$name = $q['attachment'];
+
+
+	if ( '' != $p)
+		$where .= " AND comment_post_ID = '$p'";
+	
+	if ( '' != $name) {
+		$needposts = true;
+		$where .= " AND $wpdb->posts.post_name = '$name'";
+	}
+
+	if ( !empty($q['author']) ) {
+		$needposts = true;
+		$where .= " AND $wpdb->posts.post_author='{$q['author']}'";
+	}
+
+	if ( '' !== $q['author_name'] ) {
+		$needposts = true;
+		$author = get_user_by('slug', $q['author_name']);
+		if ( $author !== false ) 
+			$where .= " AND $wpdb->posts.post_author='$author->ID'";
+	}
+
+	if ( '' !== $q['cat'] || '' !== $q['category_name'] ) {
+		if ( !empty($q['cat']) ) {
+			$cat = $q['cat'];
+		} else {
+			if ( $term = get_term_by('name', $q['category_name'], 'category') !== false )
+				$cat = $term->term_taxonomy_id;
+		}
+		
+		if ( isset($cat) ) {
+			$join =  "INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)";
+			$where .= " AND $wpdb->term_relationships.term_taxonomy_id IN ($cat)";
+
+			$needposts = true;
+		}
+	}
+
+	if($needposts == true)
+		$join = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )" . $join;
+
+	$lastcommentmodified = $wpdb->get_var("$select FROM $wpdb->comments $join $where ORDER BY comment_date_gmt DESC LIMIT 1");
 	$cache_lastcommentmodified[$timezone] = $lastcommentmodified;
 
 	return $lastcommentmodified;
