The get_comments() function, which is simply a wrapper for the WP_Comment_Query class, allows you to easily query comments posted by a specific user, but what if you wanted to get comments posted from multiple users? Currently, you have two options: a custom database query or using a filter to modify the WHERE clause of the query get_comments() generates.Let’s look at both methods.

Using a custom query

Crafting a custom query to retrieve comments from an array of specific users is not hard, thanks to the $wpdb class. A basic comment query to get the last 10 approved comments would look like this:

1
2
global $wpdb;
$comments = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE comment_approved = '1' Limit 0, 10" );

To get the last 10 approved comments from a specific user, the query would look like this:

1
2
3
global $wpdb;
$user_id  = 57;
$comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_approved = '1' AND user_id = '%d' Limit 0, 10", $user_id ) );

We can then modify this to retrieve comments from an array of users like this:

1
2
3
global $wpdb;
$user_ids = array( 57, 105, 567, 2820 );
$comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_approved = '1' AND user_id IN(%s) Limit 0, 10", $user_ids ) );

This will work just fine, though it’s advisable that, if you are to do this, you also include some form of caching. A simple cached version would be similar to this:

1
2
3
4
5
6
7
8
global $wpdb;
$user_ids = array( 57, 105, 567, 2820 );
$key      = md5( serialize( $user_ids ) );
$comments = get_transient( $key );
if( false === $comments ) {
	$comments = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->comments WHERE comment_approved = '1' AND user_id IN(%s) Limit 0, 10", $user_ids ) );
	set_transient( $key, $comments, 10800 ); // Cache for 3 hours
}

Using a filter to modify the WHERE clause

An alternative method for querying comments from an array of users is to use the comments_clauses filter, which allows us to directly modify the WHERE clause of the SQL query generated by get_comments(). This method works by calling add_filter() just before we call get_comments():

1
2
3
4
5
6
7
8
9
 
add_filter( 'comments_clauses', 'pw_comments_by_users', 10, 2 );
$comments = get_comments();
remove_filter( 'comments_clauses', 'pw_comments_by_users' );
 
function pw_comments_by_users( $clauses, $wp_comment_query ) {
	$clauses['where'] .= ' AND user_id IN (57,105,567,2820)';
    return $clauses;
}

There are two things to note here:

1. We call remove_filter() after we have called get_comments() in order to ensure we don’t accidentally modify other comment queries taking place on the page after ours.

2. We have to create a callback function for the filter. For this, we created a function called pw_comments_by_users().


Both of these methods work just fine, though they’re not great. It’d be excellent if we could simply pass an array of user IDs to get_comments(). Luckily, with the release of WordPress 3.9 in April, we will be able to do that, per this trac ticket.

  1. Leon

    Very good thanks for share,

    how i get comments from Custom post type only ?

  2. Leon

    thank you but this function don’t allow me to get comment from more than one user