我们可以使用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抱歉措辞不佳。我会再试一次:这个查询是否遵守日期降序的排序顺序? –