2014-01-19 31 views
1

我正在構建一個聊天框,用於從設置的頁面中提取評論。它實時更新。WordPress - 獲取評論,但排除一些

那麼,如何獲得最近的50條評論,但排除了我已經顯示的評論? get_comments()不允許我排除註釋。

這是想我能夠做到:

// Array of already displayed comment id's 
$exclude = array(10, 22, 41, 80); 

$args = array (
    'number' => 50, 
    'user_id' => $user_ids, 
    'post_id' => 57, 
    'status' => 'approve', 
    'order' => 'DESC', 
    'exclude' => $exclude 
); 
$comments = get_comments($args); 

任何人都知道的方式來實現「排除」參數,或等效的東西嗎?

+0

如何過濾(刪除評論的對象)?這隻需要一個循環。 –

+0

如何通過查詢從數據庫中獲取評論? –

+0

@MKhalidJunaid通過查詢從數據庫中獲取評論是可行的。 –

回答

0

您使用CA WPDB類

SELECT 
    * 
FROM 
    `wp_comments` 
WHERE `comment_approved` = 1 
    AND `comment_post_ID`=57 
    AND `user_id` IN (0, 1, 2) 
    AND comment_ID NOT IN (10, 22, 41, 80) 
    ORDER BY comment_date DESC 
    LIMIT 50 

嘗試原始的SQL查詢檢索後使用wpdb

global $wpdb 
$sql="SELECT 
    * 
FROM 
    $wpdb->comments 
WHERE `comment_approved` = 1 
    AND `comment_post_ID`=57 
    AND `user_id` IN (". join(',',$user_ids) .") 
    AND comment_ID NOT IN (". join(',',$exclude) .") 
    ORDER BY comment_date DESC 
    LIMIT 50"; 
$comments=$wpdb->get_results($sql); 


if(!empty($comments)){ 
foreach($comments as $c){ 
$single_comment=get_comment($c->comment_ID); 
} 
}