获取用户对帖子的所有评论 [英] Get all comments on a post by an array of users

查看:81
本文介绍了获取用户对帖子的所有评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图从一系列用户那里获取帖子中的所有评论。

I'm trying to get all the comments from a post by an array of users.

这就是我想做的事情:

$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?

推荐答案

我已经基于 查询方法 WP_Comment_Query 类。并根据此论坛帖子

I've built a WPDB query based on the query method of WP_Comment_Query class. And doing the sanitization based on this forum post.

global $wpdb;

// Sanitize
$post = '1148';
$post = absint($post);

// Sanitize
$a = '2'; // User One
$b = '3'; // User Two
$user_ids = array_map( 'absint', array( $a, $b ) );
$user_ids = implode( ', ', $user_ids );

$query = "SELECT * FROM $wpdb->comments 
        WHERE comment_post_ID = $post 
        AND user_id IN ($user_ids) 
        AND comment_approved = 1
        ORDER BY comment_date DESC";
$comments = $wpdb->get_results( $query );

这篇关于获取用户对帖子的所有评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆