WP_Comment_Query pagination, delving into the unknown

Yes, it is possible, but it is a bit of a pain.

Looking at the codex page, only arguments of note are number and offset.

We need these two to create our paginated pages.

First, we set the $paged parameter, which is the current page:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

Then number of comments to display:

$number = 3;

After that, calculate the offset (where the query begins pulling comments):

$offset = ( $paged - 1 ) * $number;

Let’s place them all in the arguments variable:

// arguments to filter comments
$args = array(
    'number' => $number,
    'offset' => $offset,
    'paged' => $paged
);

Now let’s hit the query and loop through it:

// the query
$the_query = new WP_Comment_Query;
$comments = $the_query->query( $args );

// Comment Loop
if ( $comments ) {

    foreach ( $comments as $comment ) {
        echo '<p>' . $comment->comment_content . '</p><br><br>';
    }

} else {

    echo 'No comments found.';

}

Excellent. Now we can test to add /page/2 to the URL bar to check if it works, which it does.

The only thing missing is adding pagination links, for example, the next_posts_link() function.

The problem is that I have not found a way to get max_num_pages. The following would normally work in a simple WP_Query:

$the_query->max_num_pages

But it does not work here. Without knowing the maximum amount of pages, we cannot create our pagination links properly. Note that count($comments) will give us only the total amount of comments per page ($number).

Personally, I fixed this by calculating the maximum pages by the custom query I was targetting. I wanted to get the total amount of comments by User ID. So I used that number like this:

$maximum_pages = $user_total_comments / $number;

This works in my case, but we definitely need a way to get max_num_pages. Hopefully, with this answer, it will inspire someone to solve the final bit. At least we have a lot more information about pagination with wp_comment_query() here than anywhere else.

Update

So the remaining problem was the lack of max_num_pages. One way to solve this is to count() the returned array of post and check if it matches with the $number (what you set in the number array key), see below:

$tot_returned_comments = count($comments);

if ($number == $tot_returned_comments) {
    echo '<a href="https://wordpress.stackexchange.com/comments/page/" . $nextpage . '">Next</a>';
}

This works in all cases besides if the final page has the exact same amount of posts as you set in your $number variable. So, if you set $number to 15, and your final paginated page has 15 results, then it will display the next button.

Now, if you do not care about performance, you could easily just run two queries. One where you do not limit the results and simply count() the results and use that count for the max_num_pages value.

While this doesn’t solve the lack of getting max_num_pages, it should be enough to get you on your feet with a fully working pagination for wp_comments_query().

Leave a Comment