0

I'm using paginate_comments_links() to get all the comments by the current user:

<?php
global $current_user;
get_currentuserinfo();
$userid = $current_user->ID;

$args = array(
    'user_id' => $userid,
   'number' => 2,
);
$comments = get_comments($args);
foreach($comments as $comment) :
    echo('<br />' . $comment->comment_date . '<br />' . $comment->comment_content);
endforeach;

paginate_comments_links();
?>

However, the pagination is not showing up. I have posted 7 comments, so I expected to see 2 comments per page, 4 pages in total. However, no pagination is showing up at all.

Turning on errors in php reveals: Notice: Undefined property: WP_Query::$comments in /wp-includes/comment.php on line 788

Where am I going wrong?

1 Answer 1

2

paginate_comment_links() actually does some magic setup for comments and then calls the standard wordpress paginate_links(). I believe that part of that magic utilizes the result of wp_list_comments().

So even though your code is works great - you cannot use the built-in paginate comments functionallity because you are not using wp_list_comments(). And you can't use that function since it only gets comments for a specific post or page which is not what you are trying to do anyways...

The solution is to use paginate_links() as that method is actually very flexible. In order to do this you need to know 2 things - what page you are on, and how many pages there are total. To accommodate this, we need to get all comments from get_comments() which isn't optimal but we work with what we've got. Here is a mental picture of what the code might look like (completely untested so no warranty - sorry):

<?php
$comments_per_page = 5;
$current_page = max( 1, get_query_var('paged') );

global $current_user;
get_currentuserinfo();
$userid = $current_user->ID;

$args = array('user_id' => $userid, 'number' => 0);
$comments = get_comments($args);

$total_comments = count($comments);
$total_pages = (($total_comments - 1) / $comments_per_page) + 1;

$start = $comments_per_page * ($current_page - 1);
$end = $start + $comments_per_page;

// Might be good to test and make sure there are comments for the current page at this point!

for($i = $start; $i < $end; $i++) {
    echo('<br />' . $comment->comment_date . '<br />' . $comment->comment_content);
}

if($total_pages > 1) {
    $args = { 'total'=>$total_pages, 'current'=>$current_page };
    paginate_links($args);
}
?>

That probably isn't perfect but it should help get you pointing the right direction. The only ugly part is that we are reading all comments with the query instead of limiting it to only the comments on the page we want - but we don't have another good way to get the number of comments that exist for the user as (get_comment_count is post based).

2
  • Thats great, Ed. Thanks for putting that solution together. The approach works like a charm.
    – Chris
    Commented Jan 12, 2013 at 16:06
  • since this has been posted WP has deprecated the use of get_currentuserinfo();. Make sure you swap that out for wp_get_current_user
    – samuelkobe
    Commented Jan 11, 2021 at 20:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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