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:
$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?
I’ve built a WPDB query based on the
query
method ofWP_Comment_Query
class. And doing the sanitization based on this forum post.A simple
SELECT
query would do:Hope this helps.
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:
Now, we can make this dynamic and processable by PHP:
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.
paste this code in
functions.php
use this function anywhere you want by calling it required parameters user ids and post id.
https://wordpress.stackexchange.com/questions/105010/get-comments-only-for-certain-specific-users-in-template-file
As Brasofilo already provided you the custom query to get the comments but it will fetch all the comments while they were trashed