MySQL - Skip Duplicate WordPress Entries
        Posted  
        
            by 55skidoo
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by 55skidoo
        
        
        
        Published on 2008-11-20T19:04:30Z
        Indexed on 
            2010/04/19
            2:53 UTC
        
        
        Read the original article
        Hit count: 423
        
I'm writing a script to display the 10 most recently "active" WordPress blog posts (i.e. those with the most recent comments). Problem is, the list has a lot of duplicates. I'd like to weed out the duplicates. Is there an easy way to do this by changing the MySQL query (like IGNORE, WHERE) or some other means? Here's what I have so far:
<?php
function cd_recently_active() {
    global $wpdb, $comments, $comment;
    $number = 10; //how many recently active posts to display? enter here
if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
    $comments = $wpdb->get_results("SELECT comment_date, comment_author, comment_author_url, comment_ID, comment_post_ID, comment_content FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
    wp_cache_add( 'recent_comments', $comments, 'widget' );
}
?>
© Stack Overflow or respective owner