wordpress中是否有一个函数可以捕获对帖子发表评论的所有用户(用户ID)?我有可用的post id。
如何查找一篇帖子中所有评论者的用户ID
2 个回复
最合适的回答,由SO网友:Mridul Aggarwal 整理而成
Poulomi Nag给出的get\\u comments()答案是正确的。这样会更有效率。
global $wpdb, $post;
$query = sprintf("SELECT user_id
FROM {$wpdb->comments}
JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->comments}.comment_post_ID
WHERE comment_post_ID = %d
AND comment_approved = \'1\'",
$post->ID);
$authors = $wpdb->get_col($query);
$authors = array_unique($authors);
$authors = array_diff($authors,array(\'0\')); // Remove those where users are not registered
另一种选择是使用WP\\u Comment\\u Queryhttp://core.trac.wordpress.org/browser/branches/3.2/wp-includes/comment.php#L186SO网友:Poulomi Nag
以下代码段是一种方法:
$args = array(
\'status\' => \'approve\',
\'post_id\' => get_the_ID()
);
$comments = get_comments( $args );
foreach( $comments as $comment )
echo $comment->user_id;
当然,应该更好地使用用户ID,而不仅仅是echo
ing。结束