构造SQL查询:
您可以尝试以下操作:
/**
 * Fetch comments\' IDs for the most recently commented posts 
 * (only one comment per post)
 *
 * @param  integer $count
 * @return array  
 */
function get_cids_wpse ( $count = 5 )
{
    global $wpdb;
    $sql = "SELECT MAX( comment_ID ) as cid 
            FROM {$wpdb->comments} 
            GROUP BY comment_post_ID
            ORDER BY cid DESC
            LIMIT 0, %d"; 
    return $wpdb->get_cols( $wpdb->prepare( $sql, $count ) );
}
 其中,我假设注释的日期与ID的顺序相同。如果您的设置不是这样,那么我们需要进一步调整。
用法示例:
如果我们需要获得每篇文章的最后5条评论,我们可以使用:
$cids = get_cids_wpse( 5 );
$comments = array();
foreach( (array) $cids as $cid )
{
    $comments[] = get_comment( $cid );
}
 或此设置:
$args = array(
    \'include\'     => get_cids_wpse( 5 );,
    \'orderby\'     => \'_include\',           //<-- custom parameter
);
$comments = get_comments( $args );
 我们使用的位置
this code snippet 按包含值排序。
注意:您还可以进一步扩展此示例,添加更多注释db字段,然后使用get_results() 方法而不是get_col(). 然后,您将只使用一个db查询来获取所有注释数据。