Last active
April 27, 2020 06:07
-
-
Save kashmiry/3c7bdc32113321b35e4619cbc2f65de1 to your computer and use it in GitHub Desktop.
Wordpress: Order comments by custom_meta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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