I need to order the comments in the admin by a custom date field; named 'datum_van_uitvoering'.
At the front-end i did the trick with get_comments. But now i have to order the backend ass well and for what i know i have to do it with pre_get_comments.
So far, i have
add_filter('pre_get_comments','display_comments_ordered_by_metadate');
function display_comments_ordered_by_metadate($query){
global $pagenow;
if($query->is_admin) {
$query->query_vars['meta_query'] = [
[
'order' => 'DESC',
'orderby' => 'meta_value_num',
'meta_key' => 'datum_van_uitvoering',
]
];
}
}
But it's not yet working... Any advice here? Thanks
I need to order the comments in the admin by a custom date field; named 'datum_van_uitvoering'.
At the front-end i did the trick with get_comments. But now i have to order the backend ass well and for what i know i have to do it with pre_get_comments.
So far, i have
add_filter('pre_get_comments','display_comments_ordered_by_metadate');
function display_comments_ordered_by_metadate($query){
global $pagenow;
if($query->is_admin) {
$query->query_vars['meta_query'] = [
[
'order' => 'DESC',
'orderby' => 'meta_value_num',
'meta_key' => 'datum_van_uitvoering',
]
];
}
}
But it's not yet working... Any advice here? Thanks
Share Improve this question asked Aug 21, 2017 at 16:57 DavelDavel 111 bronze badge 3- try replacing meta_value_num with meta_value_datetime or meta_value_date – inarilo Commented Aug 21, 2017 at 19:37
- thanks for the suggestion, but it didn't work. – Davel Commented Aug 22, 2017 at 16:45
- use add_action instead of add_filter – inarilo Commented Aug 22, 2017 at 21:26
1 Answer
Reset to default 0I found the solution and happy to share the code:
function cws_pre_get_comments( $comments ) {
$comments->query_vars['meta_key'] = 'datum_van_uitvoering';
$comments->query_vars['orderby'] = 'meta_value_num';
$comments->query_vars['order'] = 'DESC';
$comments->meta_query->parse_query_vars( $comments->query_vars );
}
add_action( 'pre_get_comments', 'cws_pre_get_comments' );