简体   繁体   English

仅在 Wordpress 中返回带有评论的帖子

[英]Only return posts with comments in Wordpress

I'm surprised I cannot find much on this topic, perhaps I'm confused.我很惊讶我在这个主题上找不到太多东西,也许我很困惑。

I am creating an archive page of comments.我正在创建一个评论存档页面。 I want to get all posts with comments where I will list the post title followed by the all the comments for that post underneath using wp_list_comments or WP Comment Query.我想获得所有带有评论的帖子,我将列出帖子标题,然后使用 wp_list_comments 或 WP Comment Query 列出该帖子的所有评论。

My question is how can I filter WP_Query() so I can only return posts that have comments.我的问题是如何过滤 WP_Query() 以便我只能返回有评论的帖子。

I've tried to modify this approach list posts with no comments - postpostmodern's answer but I want to do the exact opposite.我试图修改这种没有评论的方法列表帖子- postpostmodern 的答案,但我想做完全相反的事情。

Here is the function:这是函数:

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'  );

And then I'm attempting to use it in my loop, here is an excerpt:然后我试图在我的循环中使用它,这是一个摘录:

$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();

I guess the bit that is tripping me up is AND {$wpdb->posts}.comment_count != %d I want it to return comment_count not equal to 0 or whatever allows me to get only posts with comments in my WP_Query($args);AND {$wpdb->posts}.comment_count != %d我失望的一点是AND {$wpdb->posts}.comment_count != %d我希望它返回comment_count不等于 0 或任何允许我在我的WP_Query($args);

Also I'm not sure where to put remove_filter( 'posts_where', 'filter_comment_count' );另外我不知道把remove_filter( 'posts_where', 'filter_comment_count' ); ? ?

I realise there is 'orderby' => 'comment_count' but that doesn't exactly help my situation as far as I understand.我意识到有'orderby' => 'comment_count'但这并不能完全帮助我理解我的情况。

Might not give the exact results you want, nor be efficient, but this is all I can think of:可能不会给出您想要的确切结果,也不会有效,但这就是我能想到的:

Inside your loop, add:在循环中,添加:

$comments = get_comments(array('post_id' => $post->ID));
if ($comments !== '' && !empty($comments)) {
// The rest.
}

And that should output only the posts that have comments associated with them.这应该只输出有评论的帖子。

Edit:编辑:

There's also还有

    if( 0 < $post->comment_count ){

        // Output

    }

Which I retrieved from here: https://wordpress.stackexchange.com/questions/125577/only-display-posts-with-comments我从这里检索的: https : //wordpress.stackexchange.com/questions/125577/only-display-posts-with-comments

Old question but if anyone stumbles on this as I did, WordPress 4.9 added 'comment_count' parameter to WP_Query.老问题,但如果有人像我一样偶然发现这个问题,WordPress 4.9 会向 WP_Query 添加“comment_count”参数。

The value can either be an integer of the number of comments you require or an array where the first argument is the 'value' and the second is a 'compare' search operator.该值可以是您需要的评论数量的整数,也可以是一个数组,其中第一个参数是“值”,第二个参数是“比较”搜索运算符。

To only return posts with comments :只返回带有评论的帖子:

$args = array(
    'comment_count'  => array(
        'value'   => 0,
        'compare' => '>',
    ),
    'pagination'     => true,
    'posts_per_page' => 10,
);
$the_query = WP_Query( $args );

See https://developer.wordpress.org/reference/classes/wp_query/#comment-parameters .请参阅https://developer.wordpress.org/reference/classes/wp_query/#comment-parameters

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM