I am trying to display a list of comments a user has made on his author page, with pagination links.

First I created a simple function in functions.php to get the author's number of comments.

function commentCount() { global $wpdb; $author = get_user_by( 'slug', get_query_var( 'author_name' ) ); $count = $wpdb->get_var('SELECT COUNT(comment_ID) FROM ' . $wpdb->comments. ' WHERE comment_author_email = "' . $author->user_email . '"'); return $count; }

Doing a var_dump(commentCount()) in author.php returns the correct number of comments of that particular user which is 26.

Next, I created a custom query using WP_Comment_Query() to retrieve the author's comment content.

$comments_per_page = 10; /*Count comments*/ $all_comments_approved = commentCount(); /*Get Current Page Var*/ $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; /*How many comments offset*/ $offset = (($paged-1) * $comments_per_page) ; /*Max number of pages*/ $max_num_pages = ceil( $all_comments_approved / $comments_per_page ); $args = array( 'user_id' => $author->ID, // comments by this user only 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'post', 'number' => $comments_per_page, 'offset' => $offset ); $comments_query = new WP_Comment_Query; $comments = $comments_query->query( $args ); // Comment Loop if ( $comments ) { foreach ( $comments as $comment ) { echo $comment->comment_content . '<br>'; } /*Set current page for pagination*/ $current_page = max(1, get_query_var('paged')); /*Echo paginate links*/ echo paginate_links(array( 'base' => get_pagenum_link(1) . '%_%', 'current' => $current_page, 'total' => $max_num_pages, 'prev_text' => __('« Previous'), 'next_text' => __('Next »'), 'end_size' => 2, 'mid-size' => 3 )); } else { echo 'No comments found.'; }

The above code is placed in author.php and it retrieves the comments, and displays them 10 per day as $comments_per_page specifies. And I can see the pagination links at the end, I see 3 pages.

If I navigate to the second page, I see 10 other comments. However if I navigate to page 3, I get 404 Page Not Found

So basically, I can only see 20 comments out of the 26.

What am I missing here?

EDIT: This is the HTML code that is being outputted by paginate_links()