2013-07-14 38 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"); 
相关问题