最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Order comments in admin by custom date

programmeradmin1浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

I 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' );
发布评论

评论列表(0)

  1. 暂无评论