Skip to content

Instantly share code, notes, and snippets.

@kashmiry
Last active April 27, 2020 06:07
Show Gist options
  • Save kashmiry/3c7bdc32113321b35e4619cbc2f65de1 to your computer and use it in GitHub Desktop.
Save kashmiry/3c7bdc32113321b35e4619cbc2f65de1 to your computer and use it in GitHub Desktop.
Wordpress: Order comments by custom_meta
<?php
#order comments by custom_meta
add_filter('comments_clauses','comments_order_by_meta');
function comments_order_by_meta($clauses){
global $wpdb;
#Join comments meta
$clauses['join'] = "
LEFT JOIN $wpdb->commentmeta
ON ( $wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id ) AND ($wpdb->commentmeta.meta_key = 'kash_reviews_rating')
";
#Order By
$current_order = get_option('default_comments_page') == 'newest' ? 'DESC' : 'ASC'; //Based on options selected in wp-admin
$clauses['orderby'] = "
meta_value $current_order,
comment_date_gmt
";
return $clauses;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment