Objective
Kind feedback within the "edit-comments.php" desk based mostly on a remark meta area after clicking on the title of a customized made column.
Context
For the sake of brevity let’s assume that when a remark is posted, a meta area referred to as "hearts" is assigned to it so that each one feedback have that area which is a optimistic int from 0 to 10. Of the daring steps talked about under, the primary three are given for context and work as meant, the issue is on the 4th step.
1. Creating the column
add_filter( 'manage_edit-comments_columns', 'hearts_add_comments_column' );
operate hearts_add_comments_column( $cols ) {
$cols['hearts'] = 'Hearts';
return $cols;
}
2. Populating the column
add_action( 'manage_comments_custom_column', 'hearts_column_content', 10, 2 );
operate hearts_column_content( $column, $comment_ID ) {
swap ( $column ) :
case 'hearts' : {
$hearts = get_comment_meta( $comment_ID, 'hearts', true );
echo $hearts;
break;
}
endswitch;
}
3. Making the column sortable
add_filter( 'manage_edit-comments_sortable_columns', 'hearts_make_sortable' );
operate hearts_make_sortable( $cols ) {
$cols['hearts'] = 'by_hearts';
return $cols;
}
4. Kind based mostly on remark meta
That is the place the issues begin, one thing wants to vary within the following code which when examined independently, returns a sorted listing of feedback as meant:
add_action( 'pre_get_comments', 'hearts_orderby' );
operate hearts_orderby( $comments_query ) {
$orderby = $comments_query->query_vars['orderby'];
if( 'by_hearts' == $orderby ) {
$comments_query->question(array(
'meta_key' => 'hearts',
'orderby' => 'meta_value_num'
));
}
}
Important Error when clicking on the column’s title "Hearts" to type the column. Redirection to an empty web page with the next message:
Issues tried
The code under offers no errors, the feedback are listed however will not be sorted. If i take away the code under totally, the outcome is identical. The code is predicated on the final snippet of this web page.
add_action( 'pre_get_comments', 'hearts_orderby' );
operate hearts_orderby( $comments_query ) {
$orderby = $comments_query->query_vars['orderby'];
if( 'by_hearts' == $orderby ) {
$comments_query->set('meta_key','hearts');
$comments_query->set('orderby','meta_value_num');
}
}
Questions
- Is it doable to one way or the other alter the
hearts_orderby()
operate within the 4th step to get the specified final result? - Is it doable to make use of a unique hook within the 4th step?