コメントデータの取得を操作する comments_template_query_args

wp-inclludes/comment-template.phpのなかにコメントの取得とソートに関する変数 &comment_argsがあります。

$comment_args = array(
 'orderby'                   => 'comment_date_gmt',
 'order'                     => 'DSC',
 'status'                    => 'approve',
 'post_id'                   => $post->ID,
 'no_found_rows'             => false,
 'update_comment_meta_cache' => false, // We lazy-load comment meta for performance.
);

このあといろいろごにょごにょして。
コメントのデータを取得しているようです。

フックの一覧を見るとcomments_template_query_argsというのがあるので(version4.5~)ここにフックしてorderbyやorderを変更できそうです。
あと未承認のコメントの表示とかもできそうです。

wp-inclludes/comment-template.phpの該当箇所が(↓)
現在のバージョンは5.1なので1444行目にありますね。

 $comment_args  = apply_filters( 'comments_template_query_args', $comment_args );
 $comment_query = new WP_Comment_Query( $comment_args );
 $_comments     = $comment_query->comments;

コメントを表示する場所で↑を変更します。

//コメント取得のための変数を納めた配列
function edit_comment_query( $comment_args ) {
 $comment_args['order'] = 'ASC';
  return $comment_args;
}
//comments_template_query_argsへフックする
add_filter( 'comments_template_query_args', 'edit_comment_query' );

とりあえず今日はここまで。。。

コメント

タイトルとURLをコピーしました