0

EDIT: Updated function to use get_comments offset param. Issue is still occurring.

I am displaying 5 comments at the end of an article and using ajax to request the rest of the comments. On this page, http://www.universityaffairs.ca/opinion/in-my-opinion/phd-studies-in-canada-a-dilemma-for-international-students/#comments

the comment by Joshan Rodrigues is a reply to another comment. This is fine.

When I load the rest of the comments through ajax by clicking the More Comments link, Joshan Rodrigues' comment appears again at the end of the list, even if the comment date is older. Give it a try.

Below is function called by ajax.

function get_more_comments() {
global $wpdb; // this is how you get access to the database

$comments = get_comments( 
                array(
                    'post_id'   => $_REQUEST['post_id'],
                    'status'    => 'approve',
                    'order'     => 'ASC',
                    'offset'    => 5,
                    'number'    => 1000
                )
            );

echo '<ol class="commentlist">';
wp_list_comments( 
            array(
                'avatar_size' => 0,
                'walker'    => new Walker_Comment_custom(),
            ),
            $comments
        );
echo '</ol>';

die(); 
}

Any idea why this is happening?

  • You are getting all comments by date ascending order, storing them in an array and stripping out the first 5 from that array. The comment of the issue is not in the first 5. – cybmeta Jan 22 '15 at 16:26
  • You got me on the right track, but the comment is not necessarily #6 so it is still appearing currently even if I slice off 7 elements off the array. I should be able to remove all comments and sub-comments from the list generated by ajax. – Bundarr Jan 22 '15 at 16:33
  • I said 6 at first time but I edited the comment quickly, not sure if you saw the comment before. If you read again I don't say that the comment is the number 6, I just said it is not in the first 5. In fact, it is the last one in date ascending order. – cybmeta Jan 22 '15 at 17:42
  • The issue is that the offset parameter applies only you the most recent comments. Since I was ordering comments in ascending, I wanted the 5 oldest comments to be removed. – Bundarr Jan 23 '15 at 17:04
0

I think that a good way to get what you are trying to do is to use comment__not_in argument of get_comments() function. Send the IDs of the comments currently being displayed in the Ajax request and use them in the comment__not_in argument. For example, you could send those IDs as comment__not_in[] query var (array format):

$comment_not_in = $_REQUEST['comment__not_in'];
$comments = get_comments( 
                array(
                    'post_id'         => $_REQUEST['post_id'],
                    'status'          => 'approve',
                    'order'           => 'ASC',
                    'comment__not_in' =>  $comment_not_in,
                    'number'          => 1000
                )
            );

Also, I recommend to sanitize the data. As IDs are expected to be integer numbers we can do something like this:

$comment_not_in = array_map( 'intval', $_REQUEST['comment__not_in'] );
$comments = get_comments( 
                array(
                    'post_id'         => (int) $_REQUEST['post_id'],
                    'status'          => 'approve',
                    'order'           => 'ASC',
                    'comment__not_in' =>  $comment_not_in,
                    'number'          => 1000
                )
            );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.