Get recent comments from a specific array of users?
-
Oddly, in comment queries, you cannot pass arrays as user_id. You need to alter the query string directly by removing the user_id element from the arguments array and instead hook ‘comments_clauses’ and do something like this in the callback:
$query['where'] .= ' AND user_id IN (14,56,58,200)';I’ve just written up a more in-depth tutorial that shows how to do it: http://pippinsplugins.com/get-comments-array-user-ids/
Also, support for passing an array of user IDs to get_comments() will be added in WordPress 3.9: https://core.trac.ww.wp.xz.cn/ticket/27064
You have no idea how grateful I am for your help Pippin! Your tutorial was exactly what I needed. Thank you, thank you, thank you! 🙂
Here’s my working snippet (adjusted to only show 5 most recent) for anyone who needs it.
<?php //Many thanks to Pippin Williamson -http://pippinsplugins.com/get-comments-array-user-ids/ add_filter( 'comments_clauses', 'pw_comments_by_users', 10, 2 ); $comments = get_comments('number=5'); remove_filter( 'comments_clauses', 'pw_comments_by_users' ); function pw_comments_by_users( $clauses, $wp_comment_query ) { $clauses['where'] .= ' AND user_id IN (14,56,58,200)'; return $clauses; } if ( $comments ) { $output.= "<ul class=lastcomments>\n"; foreach ( $comments as $c ) { $output.= '<li>'; $output.= '<a href="'.get_comment_author( $c->comment_ID ).'">'; $output.= get_comment_author($c->comment_ID); $output.= '</a><p>'; $output.= get_comment_excerpt($c->comment_ID); $output.= '</br><span>Posted in: <a href="'.get_comment_link( $c->comment_ID ).'">'; $output.= get_the_title($c->comment_post_ID); $output.= '</a>'; $output.= ' on '. mysql2date('m/d/Y', $c->comment_date, $translate); $output.= "</span></p></li>\n"; } $output.= '</ul>'; echo $output; } else { echo "No comments made";}?>Happy to help!
The topic ‘Get recent comments from a specific array of users?’ is closed to new replies.
(@acasmi)
12 years, 4 months ago
Hi, I am trying to display a list of recent comments from several specific users (in my case user ids 14,56,58,200) who form a team. On their team page I simply want to list their recent comments.
I feel like I’m nearly there, as on each team members individual page I run a query that displays that one team members comments, using this code:
All attempts to display multiple users comments, however (using an array for user_id for example) have failed so far. The formatting of the list needs to be the same, just with 5 specified users instead of 1.
I have hit a coding wall. Any help would be greatly appreciated!