Alter the Main WP_Comment_Query (comments at the bottom of posts)?

别等时光非礼了梦想. 提交于 2019-12-11 13:28:32

问题


My plugin retrieves a few comments at the beginning of the post through my own WP_Comment_Query. I save these IDs so I can then alter the WP_Comment_Query request and not fetch these IDs.

When I use the pre_get_comments hook to hide these already-fetched IDs, they are also hidden from my first query at the beginning of each post. It defies the point.

$this->loader->add_action( 'pre_get_comments', $plugin_public, 'hide_the_comments' );

public function hide_the_comments( $comment_query ) {

    $comment_query->query_vars['comment__not_in'] = $the_ids_to_hide;
}

How can we target the bottom request only, just like there is is_main_query() for the post loop?


回答1:


  • Create a private variable, eg private $count = 0;
  • Increment it each time your function is run
  • Don't hide the comments if it's the first time you're running it :)



回答2:


If you need to target the "main" WP_Query_Comments() within the comments_template() core function, then the comments_template_query_args filter is available since WordPress 4.5:

$comment_args = apply_filters( 'comments_template_query_args', $comment_args );
$comment_query = new WP_Comment_Query( $comment_args );

See ticket #34442 for more info and a simple example here.



来源:https://stackoverflow.com/questions/37097405/alter-the-main-wp-comment-query-comments-at-the-bottom-of-posts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!