Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Add Pagination To WP_Comment_Query WordPress

  • SOLVED

How do i add pagination to this code?

$args = array(
// args here
);

// The Query
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );

// Comment Loop
if ( $comments ) {
foreach ( $comments as $comment ) {
echo '<p>' . $comment->comment_content . '</p>';
}
} else {
echo 'No comments found.';
}

Answers (1)

2016-10-13

Rempty answers:

Hello
You can use this code, i added some comments to the code

/*Set how many comments per page*/
$comments_per_page=2;

/*Count comments and count only approved*/
$all_comments = wp_count_comments();
/*If you are going to get only comments from a post you need to change the above code to
$all_comments = wp_count_comments($post->ID);
*/
$all_comments_approved = $all_comments->approved;

/*Get Current Page Var, if you want to show comments in the homepage will need to change get_query_var('paged') to get_query_var('page')*/
$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 = intval( $all_comments_approved / $comments_per_page ) + 1;

/*Get Comments*/
/*If you are going to get comments from a post you need to add 'post_id' => $post->ID, to the array*/
$comments = get_comments(array(
'status' => 'approve',
'number' => $comments_per_page,
'offset' => $offset
));

/*Show Comments*/
if ( $comments ) {
foreach ( $comments as $comment ) {
echo '<p>' . $comment->comment_content . '</p><br><br>';
}
} else {
echo 'No comments found.';
}

/*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' => __('&laquo; Previous'),
'next_text' => __('Next &raquo;'),
'end_size' => 2,
'mid-size' => 3
));


dev4wp comments:

You got it from here right http://wordpress.stackexchange.com/questions/184449/wp-comment-query-pagination-delving-into-the-unknown

Multiple errors.

Only works when set 1 comment per page but then displays "No comments found" on the last paginated page.

If set 2 comments per page, it removes the 1st comment. If set 3 comments per page, it removes the 1st 2 comments.

Regardless of how many comments set, it only ever displays 1 comment per page.


Rempty comments:

Test this code

/*Set how many comments per page*/
$comments_per_page=4;

/*Count comments and count only approved*/
$all_comments = wp_count_comments();
/*If you are going to get only comments from a post you need to change the above code to
$all_comments = wp_count_comments($post->ID);
*/
$all_comments_approved = $all_comments->approved;

/*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 );



/*Get Comments*/
/*If you are going to get comments from a post you need to add 'post_id' => $post->ID, to the array*/
$comments = get_comments(array(
'status' => 'approve',
'number' => $comments_per_page,
'offset' => $offset
));

/*Show Comments*/
if ( $comments ) {
foreach ( $comments as $comment ) {
echo '<p>' . $comment->comment_content . '</p><br><br>';
}
} else {
// echo 'No comments found.';
}

/*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' => __('&laquo; Previous'),
'next_text' => __('Next &raquo;'),
'end_size' => 2,
'mid-size' => 3
));


dev4wp comments:

Thanks. Works.

How about controlling the order?

Can the comments be displayed by date?


Rempty comments:

Check this
https://codex.wordpress.org/Function_Reference/get_comments

You can modify this code

$comments = get_comments(array(
'status' => 'approve',
'number' => $comments_per_page,
'offset' => $offset
));


Example, order by date desc

$comments = get_comments(array(
'status' => 'approve',
'number' => $comments_per_page,
'offset' => $offset,
'orderby' => 'comment_date',
'order' => 'DESC'
));



dev4wp comments:

Thanks. Whats next? How do i award you the money?


Rempty comments:

Your welcome
Click on
Vote to award prize