最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

database - How to rerieve comments and their replies from DB

programmeradmin1浏览0评论

I need to get comments and their replies from database to show them as json for an android app, so how can i tell if a comment has replies (get their number ) , and how can i list replies nested with their parent comments to show

This is my code if functions.php

// Comments 
add_action('rest_api_init', function () {
  register_rest_route( 'wp/v2', 'comments/(?P<category_id>\d+)',array(
                'methods'  => 'GET',
                'callback' => 'get_latest_comments'
      ));
});

function get_latest_comments($request) {
    global $wpdb;
    $postID = $request['post_id'];
    $args = array(
            'comment' => $request['post_id']
    );

$request = "SELECT * FROM $wpdb->comments";
$request .= " JOIN $wpdb->posts ON ID = comment_post_ID";
$request .= " WHERE comment_approved = '1' AND post_status = 'publish' AND post_password =''";
$request .= " AND ID='{$postID}' and comment_parent=0";

    $list = $wpdb->get_results($request);

    return $list;

This is the output now


comment_ID  "21"
comment_post_ID "172"
comment_author  "user"
comment_author_email    "[email protected]"
comment_author_url  ""
comment_author_IP   "***.65.78.45"
comment_date    "2019-09-06 13:46:50"
comment_date_gmt    "2019-09-06 13:46:50"
comment_content "Test Comment"
comment_karma   "0"
comment_approved    "1"
...

but i would like to get a nested list with comments and replies , or at least comments and number of replies of each comment

发布评论

评论列表(0)

  1. 暂无评论