Custom WP_Comment_Query with pagination and orderby?
I'm trying to setup a custom WP_Comment_Query
which is ordered by a meta key. (It might be worth mentioning that these comments are being retrieved with AJAX.)
It all works fine, until I add in pagination into the query args.
$orderby = 'top-comments';
$args = array(
'post_id' = $post_id,
'type' = 'comment',
'status' = 'approve',
'hierarchical' = true
);
if ( $orderby == 'top-comments' ) {
$args['meta_key'] = 'comment_rating';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'ASC';
}
// $comments_query = new WP_Comment_Query;
// $comments = $comments_query-query($args);
// Works fine up until I add the pagination args
$number = 5;
$paged = 1;
$args['number'] = $number;
$args['paged'] = $paged;
$comments_query = new WP_Comment_Query;
$comments = $comments_query-query($args);
// Gets the 5 latest comments, then orders those by meta_key
The results without the pagination args works perfect, and orders the comments exactly how I want it.
However, once the number argument is set, it will retrieve the newest 5 comments, and then order them by meta_key = 'comment_rating'
.
The behaviour I expect is WP_Comment_Query first applying the orderby, and then returning the top 5 results.
What am I doing wrong?
Topic wp-comment-query meta-value pagination Wordpress
Category Web