Displaying comments and child comments from wordpress database

So I have a php file which I’m sending ajax requests to so I can output the comments for each posts based on a selection filter.

Everything works ok but the displaying of the comments.

The problem: All parent comments are displayed but some child comments
are displayed and some aren’t.

Here is the PHP code im working with:

//required to work correctly
require( dirname(__FILE__) . '/../../../wp-load.php' );

$GLOBALS['comment'] = $comment;

/*
*   Process ajax comments
*/
if (isset($_POST['commentAjaxInit'])) {
    $_SESSION['thePostId'] = $_POST['thePostId'];
}

if (isset($_POST['postCommentFilter'])) {
    $commentFilterOption = intval($_POST['postCommentFilter']);

    if ($commentFilterOption == 0) {
        $args = array(
            'post_id' => intval($_SESSION['thePostId']), // use post_id, not post_ID
            'orderby' => 'comment_date',
            'order' => 'ASC',
            'parent' => '0',
        );//'parent' => 0,

        $comments = get_comments($args);
    }
    else if ($commentFilterOption == 1) {
        $args = array(
            'post_id' => intval($_SESSION['thePostId']),
            'orderby' => 'comment_date',
            'order' => 'DESC',
            'parent' => '0',
        );//'parent' => 0,

        $comments = get_comments($args);
    }

    if ($comments) {
        foreach($comments as $comment) :
            $postCommentId = $comment->comment_ID;
            // Proceed with normal comments.
                global $post;

                //like/dislike nonce
                $nonce = wp_create_nonce("wti_like_post_vote_nonce");

                if (isset($_SESSION["likedPostId"])) {
                    $key = array_search($postCommentId, $_SESSION["likedPostId"]);

                    if($key !== false) {
                        $likedClass = 'isLiked';
                    }
                    else {
                        $likedClass = '';
                    }
                }

                $getCommentClasses = get_comment_class();

                $editLink = get_edit_comment_link();

                echo '
                    <li class="'; foreach ($getCommentClasses as $key => $val) { echo $val.' '; } echo '" id="li-comment-' . $postCommentId . '">
                    <article id="comment-' . $postCommentId . '" class="comment clearfix">
                        ' . get_avatar( $comment, 60 ) . '
                        <div class="comment-wrapper">
                            <header class="comment-meta comment-author vcard">
                                <cite><b class="fn">' . get_comment_author_link() . '</b></cite>
                                <a class="comment-time" href="' . esc_url( get_comment_link( $comment->comment_ID ) ) . '"><time datetime="' . get_comment_time( 'c' ) . '">' . esc_attr(get_comment_date(), 'onepress') . '</time></a>
                                <span class="edit-link"><a class="comment-edit-link" href="' . $editLink . '">Edit</a></span>
                            </header>
                            ';

                            if ( '0' == $comment->comment_approved ) : 
                                echo '<p class="comment-awaiting-moderation">' . _e( 'Your comment is awaiting moderation.', 'onepress' ) . '</p>';
                            endif; 

                            echo '
                            <div class="comment-content entry-content">

                                <p>' . esc_html($comment->comment_content) . '</p>

                            </div>

                        <div class="watch-action commentView">
                            <div class="watch-position align-left">
                                <div class="action-like actionlike-' . esc_attr($postCommentId) . ' ' . esc_attr($likedClass) . '">
                                    <a class="lbg-style1 like-' . esc_attr($postCommentId) . ' jlk" href="javascript:void(0)" data-task="like" data-post_id="' . esc_attr($postCommentId) . '" data-nonce="' . esc_attr($nonce) . '" rel="nofollow"><span class="thumbsIcon" title="Liked it"></span>
                                    <span class="lc-' . esc_attr($postCommentId) . ' lc">' . GetWtiLikeCount($postCommentId) . '</span></a>
                                </div>
                                <div class="action-unlike actionunlike-' . esc_attr($postCommentId) . ' ' . esc_attr($dislikedClass) . '">
                                    <a class="unlbg-style1 unlike-' . esc_attr($postCommentId) . ' jlk" href="javascript:void(0)" data-task="unlike" data-post_id="' . esc_attr($postCommentId) . '" data-nonce="' . esc_attr($nonce) . '" rel="nofollow"><span class="thumbsIcon" title="Disliked it"></span>
                                    <span class="unlc-' . esc_attr($postCommentId) . ' unlc">' . GetWtiUnlikeCount($postCommentId) . '</span></a>
                                </div>
                                <div class="status-' . esc_attr($postCommentId) . ' status align-left"></div>
                            </div>
                        </div>

                    </div>

                </article>';

            $args = array( 
                'add_below' => 'comment', 
                'respond_id' => 'respond', 
                'reply_text' => 'Reply', 
                'login_text' => 'Log in to Reply', 
                'depth' => 0, 
                'before' => '' 
            );

            $onclick = sprintf( 'return addComment.moveForm( "%1$s-%2$s", "%2$s", "%3$s", "%4$s" )',
              $args['add_below'], $comment->comment_ID, $args['respond_id'], intval($_SESSION['thePostId'])
            );

            $commentReplyLink = sprintf( "<a rel='nofollow' class='comment-reply-link' href='%s' onclick='%s' aria-label='%s' data-toggle='modal' data-target='#commentsModal'>%s</a>",
              esc_url( "#comment-" . $postCommentId ),
              $onclick,
              esc_attr( sprintf( $args['reply_to_text'], $comment->comment_author ) ),
              $args['reply_text']
            );

            echo $commentReplyLink;

            /*
            *   Check for child comments and output
            */
            $args = array(
                'post_id' => intval($_SESSION['thePostId']),
                'parent' => $postCommentId,
            );

            $childComments = get_comments($args);

            if ($childComments) {
                foreach ($childComments as $childComment) {
                    $postChildCommentId = $childComment->comment_ID;

                    if (isset($_SESSION["likedPostId"])) {
                    $key = array_search($postChildCommentId, $_SESSION["likedPostId"]);

                        if($key !== false) {
                            $likedClass = 'isLiked';
                        }
                        else {
                            $likedClass = '';
                        }
                    }

                    $getCommentClasses = get_comment_class( 'depth-2', $postChildCommentId );

                        echo '
                            <ul class="children">
                            <li class="'; foreach ($getCommentClasses as $key => $val) { echo $val.' '; } echo '" id="li-comment-' . $postChildCommentId . '">
                            <article id="comment-' . $postChildCommentId . '" class="comment clearfix">
                                ' . get_avatar( $childComment->user_id, 60 ) . '
                                <div class="comment-wrapper">
                                    <header class="comment-meta comment-author vcard">
                                        <cite><b class="fn">' . get_comment_author_link( $postChildCommentId  ) . '</b></cite>
                                        <a class="comment-time" href="' . esc_url( get_comment_link( $postChildCommentId  ) ) . '"><time datetime="' . get_comment_time( $postChildCommentId, 'c' ) . '">' . esc_attr(get_comment_date( 'F d, Y', $postChildCommentId ), 'onepress') . '</time></a>
                        ';

                        $editLink = get_edit_comment_link();

                        echo '<span class="edit-link"><a class="comment-edit-link" href="' . $editLink . '">Edit</a></span>
                        </header>
                        ';

                        if ( '0' == $childComment->comment_approved ) : 
                            echo '<p class="comment-awaiting-moderation">' . _e( 'Your comment is awaiting moderation.', 'onepress' ) . '</p>';
                        endif; 

                        echo '
                            <div class="comment-content entry-content">

                                <p>' . $childComment->comment_content . '</p>

                            </div>

                            <div class="watch-action commentView">
                                <div class="watch-position align-left">
                                    <div class="action-like actionlike-' . esc_attr($postChildCommentId) . ' ' . esc_attr($likedClass) . '">
                                        <a class="lbg-style1 like-' . esc_attr($postChildCommentId) . ' jlk" href="javascript:void(0)" data-task="like" data-post_id="' . esc_attr($postChildCommentId) . '" data-nonce="' . esc_attr($nonce) . '" rel="nofollow"><span class="thumbsIcon" title="Liked it"></span>
                                        <span class="lc-' . esc_attr($postChildCommentId) . ' lc">' . GetWtiLikeCount($postChildCommentId) . '</span></a>
                                    </div>
                                    <div class="action-unlike actionunlike-' . esc_attr($postChildCommentId) . ' ' . esc_attr($dislikedClass) . '">
                                        <a class="unlbg-style1 unlike-' . esc_attr($postChildCommentId) . ' jlk" href="javascript:void(0)" data-task="unlike" data-post_id="' . esc_attr($postChildCommentId) . '" data-nonce="' . esc_attr($nonce) . '" rel="nofollow"><span class="thumbsIcon" title="Disliked it"></span>
                                        <span class="unlc-' . esc_attr($postChildCommentId) . ' unlc">' . GetWtiUnlikeCount($postChildCommentId) . '</span></a>
                                    </div>
                                    <div class="status-' . esc_attr($postChildCommentId) . ' status align-left"></div>
                                </div>
                            </div>

                        </div>
                        </ul>

                    </article>';
                }
            }
            //END check for child comments
        endforeach;

    }
}

Can anyone point me in the right direction.

Cheers

Read more here: Displaying comments and child comments from wordpress database

Leave a Reply

Your email address will not be published. Required fields are marked *