Skip to content Skip to sidebar Skip to footer

Get Latest Comment Records And The Corresponding Post Slugs In Wordpress

I have the following query for getting the latest comments: global $wpdb; $sql = 'SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comm

Solution 1:

From my 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 get back an object with $limit results or NULL.


Post a Comment for "Get Latest Comment Records And The Corresponding Post Slugs In Wordpress"