【发布时间】:2014-10-13 15:03:34
【问题描述】:
我很惊讶我在这个话题上找不到太多,也许我很困惑。
我正在创建 cmets 的存档页面。我想获取所有带有 cmets 的帖子,我将在其中列出帖子标题,然后使用 wp_list_cmets 或 WP Comment Query 在下面列出该帖子的所有 cmets。
我的问题是如何过滤 WP_Query() 以便我只能返回包含 cmets 的帖子。
我试图修改这种方法 list posts with no comments - 后现代的回答,但我想做完全相反的事情。
函数如下:
function filter_comment_count( $sql ){
global $wpdb;
$comment_count = get_query_var( 'comment_count' );
if( is_numeric($comment_count) )
$sql .= $wpdb->prepare( " AND {$wpdb->posts}.comment_count != %d ", $comment_count);
return $sql;
}
add_filter( 'posts_where', 'filter_comment_count' );
然后我试图在我的循环中使用它,这里是一个摘录:
$args = array (
'comment_count' => '0'
'pagination' => true,
'posts_per_page' => '10',
);
$the_query = new WP_Query($args);
while($the_query->have_posts()) : $the_query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
wp_reset_postdata();
我猜是 AND {$wpdb->posts}.comment_count != %d 我希望它返回不等于 0 的 comment_count 或任何允许我在我的 WP_Query($args); 中仅获得带有 cmets 的帖子
我也不知道把remove_filter( 'posts_where', 'filter_comment_count' );放在哪里?
我知道有'orderby' => 'comment_count',但据我所知,这并不能完全帮助我的情况。
【问题讨论】:
标签: php wordpress add-filter