wpdb查询注释和注释元为一

时间:2015-01-30 12:38:12

标签: php mysql sql wordpress wpdb

我正在尝试构建一个通知页面,作者可以在其中查看帖子上的所有活动。

我试图在所有作者帖子上发表评论 - 我已经工作了,但我还需要获取两个评论元键,一个是" scientific_name"另一个是" common_name"

这适用于获取评论作者,标题,内容等评论数据。但不包括重要的评论元

 $suggestions = $wpdb->get_results( "
  SELECT $wpdb->posts.guid, 
  $wpdb->posts.post_title, 
  $wpdb->posts.ID, 
  $wpdb->comments.comment_author, 
  $wpdb->comments.comment_author_email,
  $wpdb->comments.comment_date, 
  $wpdb->comments.comment_content 
  FROM $wpdb->posts left join $wpdb->comments
  ON $wpdb->posts.ID=$wpdb->comments.comment_post_ID
  WHERE $wpdb->posts.post_author=".$current_user->id." 
  AND $wpdb->posts.post_status='publish' AND 
  $wpdb->comments.comment_type='comment' 
  OR $wpdb->comments.comment_type='suggestion'
");

我也尝试了以下但是返回了非常错误的数据:

$suggestions = $wpdb->get_results( "
  SELECT * 
  FROM $wpdb->posts as a left join $wpdb->comments as b 
  ON b.comment_post_ID = a.ID left join $wpdb->commentmeta as c 
  ON c.comment_id = b.comment_ID 
  WHERE a.post_author='63' 
  AND a.post_status='publish' and b.comment_type='comment' 
  OR b.comment_type='suggestion'
");
经过大量的工作后,我得到了你所看到的内容,但是有很多循环。

$auth_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE $wpdb->posts.post_author = '".$current_user->id."' AND $wpdb->posts.post_status='publish' AND $wpdb->posts.post_type='species'");
foreach($auth_posts as $auth_post) {
    $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE $wpdb->comments.comment_post_ID = ".$auth_post->ID." AND $wpdb->comments.comment_type='comment' OR $wpdb->comments.comment_type='suggestion'");
    if($comments) {
        foreach($comments as $comment) {
            $comment_meta = get_comment_meta($comment->comment_ID);
            echo $auth_post->post_title.'<br>';
            echo $comment->comment_author."<br>";
           // print_r($comment_meta['scientific_name']);
            echo $comment_meta['scientific_name'][0].'<br>';
            echo $comment_meta['common_name'][0].'<br>';

        }
    }
}

0 个答案:

没有答案