I need to create a condition to check if defined user ( no current
user ) has comment on current post
For that purpose, you can just use get_comments()
like so:
$post_id = get_the_ID(); // or just set a specific post ID
$user_id = 444;
// Get the total number of comments by the above user on the above post only.
$comment_count = get_comments( array(
'post_id' => $post_id,
'user_id' => $user_id,
'count' => true,
) );
if ( $comment_count ) {
echo "user $user_id has <b>$comment_count comments</b> on post $post_id";
} else {
echo "user $user_id has not commented on post $post_id";
}
how can I get array of users ids who have comment in current post
Just like the above example, you can also use get_comments()
like so:
$comments = get_comments( array(
'post_id' => $post_id,
) );
// Get the users who have commented on the above post and store their ID, if any,
// in $commenters.
$commenters = array();
foreach ( $comments as $comm ) {
if ( $comm->user_id && ! in_array( $comm->user_id, $commenters ) ) {
$commenters[] = $comm->user_id;
}
}
if ( in_array( $user_id, $commenters ) ) {
.. your code here.
} else ...