I've the next query to get the most recent comments:
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url, SUBSTRING(comment_content,1,30) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10";
$comments = $wpdb->get_results($sql);
I should also obtain the title(slug) from the publish for every comment, without managing a query for every comment. Are you able to advise a modification towards the above query to do this? Even better, can one do that using built-in Wordpress functions?
Essentially I want:
$comments = array ( 'comment_object' => ... , 'post_name' => ... )
From the own Recent Comments code with minor edits:
/**
* @return object
*/
function recent_comments_query($limit)
{
global $wpdb;
$sql = "SELECT DISTINCT ID,
post_title,
post_name, // <- post name
post_password,
comment_ID,
comment_post_ID,
comment_author AS author,
comment_date_gmt,
comment_approved,
comment_type,
comment_author_url AS url,
SUBSTRING(comment_content, 1, 200)
AS comment_content
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts
ON (
$wpdb->comments.comment_post_ID = $wpdb->posts.ID
)
WHERE comment_approved = '1'
AND comment_type = ''
AND post_password = ''
ORDER BY comment_date_gmt DESC
LIMIT $limit";
return $wpdb->get_results($sql);
}
You’ll return an item with $limit
results or NULL
.