改变主WP_Comment_Query(帖子底部的评论)?

时间:2016-05-08 07:35:22

标签: wordpress wordpress-plugin wordpress-theming

我的插件通过我自己的WP_Comment_Query在帖子开头检索了一些注释。我保存这些ID,以便我可以更改WP_Comment_Query请求,而不是获取这些ID。

当我使用 pre_get_comments 挂钩隐藏这些已经获取的ID时,它们也会在每个帖子开头的第一个查询中隐藏。它无视这一点。

$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;
}

我们如何才能定位底部请求,就像post循环的is_main_query()一样?

2 个答案:

答案 0 :(得分:1)

  • 创建一个私有变量,例如private $count = 0;
  • 每次运行功能时增加
  • 如果您是第一次运行评论,请不要隐藏评论:)

答案 1 :(得分:0)

如果您需要定位" main" WP_Query_Comments()核心功能中的comments_template(),自WordPress 4.5以来,comments_template_query_args过滤器为available

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

有关详情和简单示例#34442,请参阅故障单here