Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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).

share|improve this answer
    
Thats great, Ed. Thanks for putting that solution together. The approach works like a charm. –  Chris Jan 12 '13 at 16:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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