I am aiming to pagination WP_Comment_Query(). It appears this is either taboo or there is no viable information about it online, not anything.
Why? Is it possible? If so, how?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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="/comments/page/' . $nextpage . '" rel="nofollow noreferrer noopener">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().
Method 2
The alternative way to handle this problem is to use paginate_links() with get_comments() (or any similar query). Specifically to get the equivalent of max_num_pages, you can use the built-in function wp_count_comments().
So, to get your maximum number of pages, you’d first produce a count of all the comments you want. Presuming you don’t want unapproved comments:
$all_comments = wp_count_comments(); $all_comments_approved = $all_comments->approved;
It’s a simple matter to divide $all_comments_approved by the number of comments per page. So, with a desired “n” comments per page:
//Adding one at the end as simple way of rounding up. $max_num_pages = intval( $all_comments_approved / $comments_per_page ) + 1;
(The value of $comments_per_page should be pre-set as > 0, and is also used in the main comment query – see below).
The full pagination function will look something like the following, depending on other particulars. (For example: Sometimes the ‘base’ option will be made trickier by the presence of query variables in the URL. I had to use a preg_replace to get around that problem in one application.)
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
//check codex for how to work with get_pagenum_link and variations
'base' => get_pagenum_link(1) . '%_%',
'current' => $current_page,
'total' => $max_num_pages,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
'end_size' => 2,
'mid-size' => 3
));
…and the rest is formatting the output… and the main comment query. The latter will look like the following: Note that $comments_per_page is critical here both for the number of comments retrieved and for calculating the offset. Note also that the total size of the object retrieved is limited by the number (as well as by ‘status’).
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$offset = ($paged - 1) * $comments_per_page;
$comments = get_comments(array(
'status' => 'approve',
'number' => $comments_per_page,
'offset' => $offset
));
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0