2013-07-14 55 views
1

我試圖從用戶數組獲得帖子的所有評論。獲取用戶數組對帖子的所有評論

這是想我能夠做到:

$user_ids = array(10, 22, 41, 80); 
$post_id = 57; 

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

現在這顯然行不通,但是這就是我想要做什麼。有沒有其他方法可以實現這一點?也許使用自定義選擇?

回答

1

我已經根據WP_Comment_Query類的the query method構建了一個WPDB查詢。並根據this forum post進行消毒。

global $wpdb; 

// Sanitize 
$post = '1148'; 
$post = absint($post); 

// Sanitize 
$a = '2'; // User One 
$b = '3'; // User Two 
$user_ids = array_map('absint', array($a, $b)); 
$user_ids = implode(', ', $user_ids); 

$query = "SELECT * FROM $wpdb->comments 
     WHERE comment_post_ID = $post 
     AND user_id IN ($user_ids) 
     AND comment_approved = 1 
     ORDER BY comment_date DESC"; 
$comments = $wpdb->get_results($query); 
+0

這似乎工作的偉大將取回所有的評論。它是否將最新評論置於$ comments [0]中?我認爲它確實存在,但在SELECT中沒有看到,所以只需與您覈對。 –

+0

使用'echo'

' . print_r($comments, true) . '
';'或'var_dump($ comments)'來檢查它的內容。我使用[FirePHP](http://wordpress.stackexchange.com/a/71599/12615)進行調試。 – brasofilo

+0

抱歉措辭不佳。我會再試一次:這個查詢是否遵守日期降序的排序順序? –

0

我們可以使用OR條件在MySQL做到這一點。

如果我使用你提供的這是可以做到這樣的價值觀來寫,我們希望查詢:

SELECT * FROM comments WHERE comment_post_ID='57' AND (user_id='10' OR user_id='22' OR user_id='41' OR user_id='80') 

現在,我們可以讓這個充滿活力和加工用PHP:

// The User IDs and Post ID (make sure you are escaping these properly). 
$user_ids = array(10, 22, 41, 80); 
$post_id = 57; 

foreach($user_ids as $uid){  $user_ids_string .= " OR user_id='$uid'"; }  // Loop through each user and append their ID to the query segment. 
$user_ids_string = substr($use, 4);             // Remove the unnecessary first " OR " 

$query = mysql_query("SELECT * FROM comments WHERE comment_post_ID='$post_id' AND ($user_ids_string)");  // Create the final MySQL Query. 
while($row = mysql_fetch_array($query)){ 
    // Do something with each $row[]. 
} 

在使用此功能之前,請確保您在使用此功能之前已正確連接到WordPress數據庫,並且我列出的所有表格和字段都適用於您的安裝。這個代碼functions.php

function users_comment($postid = null , $users = array()){ 
    if(! empty($users) && $postid){ 

     foreach ($users as $user) { 
      $args = array (
       'number' => '', 
       'user_id' => $user, 
       'post_id' => $postid, 
       'status' => 'approve', 
       'order' => 'DESC' 
      ); 
      $comments[] = get_comments($args); 
     } 
    return $comments; 
    }else{ 
     return 'Please provide a user id and postid'; 
    } 
} 

使用此功能,任何你想要通過調用它

0

膏所需參數的用戶ID和帖子的ID。

print_r(users_comment(1,array(1,4,3))); 
+0

[在哪裏放我的代碼:plugin或functions.php?](http://wordpress.stackexchange.com/q/73031/12615) – brasofilo

0
only single user to get post: 
$args = array(
    'status' => 'approve', 
    'number' => '-1', 
    'post_id' => 57, 
     'user_id' => 1, 
     'order' => 'DESC' 
); 
$comments = get_comments($args); 
foreach($comments as $comment) : 
    echo($comment->comment_author . '<br />' . $comment->comment_content); 
endforeach; 
?> 

對於多用戶使用後ID獲得評論:

$args = array('user_id' => 0); 
add_filter('comments_clauses', 'custom_comments_clauses'); 
$comments = get_comments($args); 
remove_filter('comments_clauses', 'custom_comments_clauses'); 

function custom_comments_clauses($clauses){ 
    $clauses['where'] = str_replace('user_id = 0', 
         'user_id IN (1, 2, 3)', 
         $clauses['where']); 
    return $clauses; 
} 

https://wordpress.stackexchange.com/questions/105010/get-comments-only-for-certain-specific-users-in-template-file

+0

您不能在get_comments中爲'user_id'參數使用數組。 –

+0

對不起,我之前添加的第一個代碼只有一個用戶的評論,所以有一個更改添加多個用戶的評論得到。 –

1

簡單SELECT查詢將做到:

$query = "SELECT * FROM $wpdb->comments 
      WHERE comment_post_ID = %d 
      AND comment_approved = 1 
      AND user_id IN %s 
      ORDER BY comment_date DESC"; // get only approved comment and sort by date 

$comments = $wpdb->get_results($wpdb->prepare(
    $query, 
    intval($post_id), 
    '('.implode(',', array_map('intval', $user_ids)).')' 
)); // use prepare to prevent SQL injection 

希望這有助於。

+0

向上投票因爲準備。但brasofilo擊敗你。雖然謝謝! –

+0

我甚至沒有用'wpdb-> prepare'進行測試,因爲[最近的線程在\ [wp-hackers \]](http://wordpress-hackers.1065353.n5.nabble.com/WP- 3-5-2-multisite-How-to-use-NOT-IN-in-wpdb-prepare-tp41812p41824.html),我認爲它也會在這種情況下出現問題。出於好奇,你測試了這個,Hieu?我試過了,它返回'0'的結果... – brasofilo

+0

不,不是真的,也可能會導致我的代碼中也有bug –

0

由於Brasofilo已經提供您的自定義查詢得到的意見,但是,當他們搗毀

$user_ids = array(10, 22, 41, 80); 
$post_id = 57; 
global $wpdb; 

$comments=$wpdb->get_results("SELECT * FROM `wp_comments` WHERE 
`comment_post_ID` =$post_id AND `user_id` IN (".join(',',$user_ids)") 
AND `comment_approved` ='1' ORDER BY `comment_date` DESC"); 
相關問題