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 );

现在显然这不起作用,但这就是我想做的。有没有其他方法可以实现这一目标?也许使用自定义选择?

4

6 回答 6

3

我已经基于类query方法构建了一个 WPDB 查询WP_Comment_Query。并根据此论坛帖子进行消毒。

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 );
于 2013-07-16T19:38:34.170 回答
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

希望这可以帮助。

于 2013-07-19T00:26:33.013 回答
0

我们可以使用ORMySQL 中的条件来做到这一点。

如果我要使用您提供的值编写我们想要的查询,可以这样做:

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 数据库,并且我列出的所有表和字段对于您的安装都是正确的。

于 2013-07-16T20:18:58.033 回答
0

将此代码粘贴到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';
    }
}

通过调用所需的参数用户 ID 和帖子 ID,在任何你想要的地方使用这个函数。

print_r( users_comment(1,array(1,4,3)) );
于 2013-07-18T07:45:53.437 回答
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

于 2013-07-18T08:06:24.500 回答
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");
于 2013-07-19T20:45:14.217 回答