admin管理员组文章数量:1122832
I have ratings on a custom comment type that stores the ratings in the comment_karma
meta of the comment. Now I want to pull up the comments for a specific user and sort them by rating. Here is what I have so far:
add_filter( 'pre_get_comments', [ $this, 'filter_comments' ] );
public function filter_comments( $query ) {
// Bail on anything not admin
if ( !is_admin() ) {
return;
}
// Only run this on the comments table
$current_screen = get_current_screen();
if ( 'edit-comments' !== $current_screen->base ) {
return;
}
// Check for user_id
$user_id = isset( $_GET[ 'user_id' ] ) ? absint( $_GET[ 'user_id' ] ) : false;
if ( $user_id ) {
// Order
$order = isset( $_GET[ 'order' ] ) ? sanitize_text_field( $_GET[ 'order' ] ) : 'DESC';
// Set the query
$query->query_vars[ 'type' ] = $this->comment_type;
$query->set( 'user_id', $user_id );
$query->set( 'orderby', 'comment_karma' );
$query->set( 'order', $order );
}
} // End filter_comments()
I am getting the correct comments from the correct user; however, it is not ordering by comment_karma
like I want. I also tried doing the following which didn't help either:
$query->set( 'meta_key', 'comment_karma' );
$query->set( 'orderby', 'meta_key_num' );
Any suggestions?
I have ratings on a custom comment type that stores the ratings in the comment_karma
meta of the comment. Now I want to pull up the comments for a specific user and sort them by rating. Here is what I have so far:
add_filter( 'pre_get_comments', [ $this, 'filter_comments' ] );
public function filter_comments( $query ) {
// Bail on anything not admin
if ( !is_admin() ) {
return;
}
// Only run this on the comments table
$current_screen = get_current_screen();
if ( 'edit-comments' !== $current_screen->base ) {
return;
}
// Check for user_id
$user_id = isset( $_GET[ 'user_id' ] ) ? absint( $_GET[ 'user_id' ] ) : false;
if ( $user_id ) {
// Order
$order = isset( $_GET[ 'order' ] ) ? sanitize_text_field( $_GET[ 'order' ] ) : 'DESC';
// Set the query
$query->query_vars[ 'type' ] = $this->comment_type;
$query->set( 'user_id', $user_id );
$query->set( 'orderby', 'comment_karma' );
$query->set( 'order', $order );
}
} // End filter_comments()
I am getting the correct comments from the correct user; however, it is not ordering by comment_karma
like I want. I also tried doing the following which didn't help either:
$query->set( 'meta_key', 'comment_karma' );
$query->set( 'orderby', 'meta_key_num' );
Any suggestions?
Share Improve this question asked Apr 4, 2024 at 18:43 AristoclesAristocles 1397 bronze badges2 Answers
Reset to default 1I think you're close with your 2nd snippet, but you should be using meta_value_num
instead of meta_key_num
.
$query->set( 'meta_key', 'comment_karma' );
$query->set( 'orderby', 'meta_value_num' );
See the WP_Query orderby
docs for more info.
With the help of @f3bruary from the WordPress Chat Discord server, the solution is this:
add_filter( 'posts_orderby', [ $this, 'orderby_comment_karma' ] );
add_filter( 'pre_get_comments', [ $this, 'filter_comments' ] );
/**
* Order by comment karma
*
* @param string $orderby
* @return string
*/
public function orderby_comment_karma( $orderby ) {
if ( isset( $_GET[ 'orderby' ] ) && 'comment_karma' == sanitize_key( $_GET[ 'orderby' ] ) ) {
$orderby = 'comment_karma';
}
return $orderby;
} // End orderby_comment_karma()
/**
* Filter the comments by user id
*
* @param object $query
* @return void
*/
public function filter_comments( $query ) {
// Bail on anything not admin
if ( !is_admin() ) {
return;
}
// Only run this on the comments table
$current_screen = get_current_screen();
if ( 'edit-comments' !== $current_screen->base ) {
return;
}
// Check for user_id
$user_id = isset( $_GET[ 'user_id' ] ) ? absint( $_GET[ 'user_id' ] ) : false;
if ( $user_id ) {
// Set the query
$order = isset( $_GET[ 'order' ] ) ? sanitize_text_field( $_GET[ 'order' ] ) : 'DESC';
$query->set( 'order', $order );
if ( isset( $query->query[ 'orderby' ] ) && 'comment_karma' == $query->query[ 'orderby' ] ) {
$query->query_vars[ 'orderby' ] = 'comment_karma';
}
// return $query;
}
} // End filter_comments()
本文标签: phpPregetcomments and orderby commentkarma
版权声明:本文标题:php - Pre_get_comments and orderby comment_karma 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310553a1934419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论