I want to sort comments by a number of likes which is saved as a meta_key in the wp_commentmeta table: I have tried two ways but it doesn't sort the comments by most liked comment at the top. Please suggest the solution:

$args = array(
    'post_type' => 'comment',
    'post_status' => 'publish',
    'callback' => 'listingo_comments',
    'meta_key' => 'cld_like_count',
    'orderby' => 'meta_value_num',
    'order'    => 'DESC'             


// and second way

$query = new WP_Query(array(
                        'post_status'   => 'publish',
                        'post_type'     => 'comment',
                        'callback'      => 'listingo_comments',
                        'orderby'       => 'cld_like_count',
                        'order'         => 'DESC'

  • Do you have a post type with the slug comment? If not, use the first method and remove the 'post_type' => 'comment',. – Sally CJ Feb 18 at 10:26
  • thanks for the answer, I do not have post type with the slug comment. I tried the first method by removing 'post_type' => 'comment' but still the order of comments does not change – Emma B Feb 18 at 11:17
  • Thanks! Sally, it sorts the comment now. But there is one problem it lists comment that has some value in cld_like_count meta_key. How can I add Zero '0' to this meta_key whenever there is a new comment. So that it lists all comments for the post and later users can like or dislike that comment. – Emma B Feb 19 at 8:03
  • $args = get_comments(array( 'post_id' => get_the_ID(), 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'cld_like_count' )); wp_list_comments(array( 'callback' => 'listingo_comments',), $args); Or can we make changes to this code to list all comments with or without having any values in cld_like_count meta_key. – Emma B Feb 19 at 8:13
  • anyone knows how can I solve this problem. – Emma B Feb 22 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.