我們可以使用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';
}
}
使用此功能,任何你想要通過調用它
這似乎工作的偉大將取回所有的評論。它是否將最新評論置於$ comments [0]中?我認爲它確實存在,但在SELECT中沒有看到,所以只需與您覈對。 –
使用'echo'
';'或'var_dump($ comments)'來檢查它的內容。我使用[FirePHP](http://wordpress.stackexchange.com/a/71599/12615)進行調試。 – brasofilo抱歉措辭不佳。我會再試一次:這個查詢是否遵守日期降序的排序順序? –