按用户角色过滤wordpress评论

时间:2017-05-17 04:02:40

标签: php mysql sql wordpress

尝试按特定用户角色过滤我的所有评论。在这种情况下,用户角色称为group1。这将仅在我删除WHERE m.meta_key = 'wp_capabilities' AND m.meta_value = 'group1'时返回所有评论 来自下面的查询。

$query = $wpdb->prepare( "
SELECT *
  FROM $wpdb->comments c
  LEFT 
  JOIN $wpdb->commentmeta cm 
    ON c.comment_ID = cm.comment_id
  LEFT 
  JOIN $wpdb->users u 
    ON c.user_id = u.ID
  LEFT 
  JOIN $wpdb->usermeta m 
    ON m.user_id = u.ID
 WHERE c.comment_post_ID = %s
   AND c.comment_approved = 1
 WHERE m.meta_key = 'wp_capabilities'
   AND m.meta_value = 'group1'
 ORDER 
    BY c.comment_ID $comment_order
                    ",
                    $post->ID);

1 个答案:

答案 0 :(得分:0)

您有两个WHERE子句,但即使修复了语法错误,它也会像inner join那样作为where子句中外表的过滤条件,将外连接转换为内部联接。

LEFT JOIN ON子句中添加外表的过滤器:

select *
from $wpdb - > comments c
left join $wpdb - > commentmeta cm on c.comment_ID = cm.comment_id
left join $wpdb - > users u on c.user_id = u.ID
left join $wpdb - > usermeta m on m.user_id = u.ID
    and m.meta_key = 'wp_capabilities'
    and m.meta_value = 'group1'
where c.comment_post_ID = % s
    and c.comment_approved = 1
order by c.comment_ID $comment_order