Get all comments on a post by an array of users

I’m trying to get all the comments from a post by an array of users.

This is what I’d like to be able to do:

Read More
$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 );

Now obviously this doesn’t work, but that’s what I’d like to do. Is there any other way to achieve this? Maybe using a custom select?

Related posts

Leave a Reply

6 comments

  1. I’ve built a WPDB query based on the query method of WP_Comment_Query class. And doing the sanitization based on 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 );
    
  2. A simple SELECT query would do:

    $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
    

    Hope this helps.

  3. We can use the OR condition in MySQL to do this.

    If I was to write the query we want using the values you provided it could be done something like this:

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

    Now, we can make this dynamic and processable by 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[].
    }
    

    Before you use this, make sure you’re connected to the WordPress database properly before using this and that all the tables and fields I’ve listed are correct for your installation first.

  4. paste this code in 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';
        }
    }
    

    use this function anywhere you want by calling it required parameters user ids and post id.

    print_r( users_comment(1,array(1,4,3)) );
    
  5. 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;
    ?>
    

    For multiple user get comment using post 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

  6. As Brasofilo already provided you the custom query to get the comments but it will fetch all the comments while they were trashed

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