WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to query out only posts that have comments. So posts with comment_count = 0 won't show but anything above that will.

This is what i query but it still picks out posts that doesen't have comments. Is there another way?

query_posts(array('comment_count' > 0, 'post_type' => 'letters', 'showposts' => 10, 'post_status' => array('publish'), 'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1), 'meta_key' => 'letter_show_on_site', 'meta_value' => "yes"));
share|improve this question
    
'comment_count' was not meant to filter posts in that manner. It was used by WP_Query only to order posts. – sri Dec 6 '13 at 10:30
    
okay, thank you for the information. Is there any other way to pick out posts with comments? – Paparappa Dec 6 '13 at 10:32

you can use WP_Query

<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
    'post_type' => 'letters',
    'posts_per_page' => 10,
    'paged' => $paged,
    'meta_key' => 'letter_show_on_site',
    'meta_value' => "yes"
);
$the_query = new WP_Query($args);
if( $the_query->have_posts()){
    while($the_query->have_posts()){
        the_post();
        global $post;

        //show post which have comment_count > 0
        if( 0 < $post->comment_count ){

            the_title();

        }
    }
}
?>
share|improve this answer

By using the query below(not tested) you will be able to get all the ids of posts that have comments.

$postids = $wpdb->get_col("SELECT distinct ID FROM $wpdb->posts, $wpdb->comments where comment_post_ID = ID ORDER BY ID");
share|improve this answer

I ran into this same problem recently and developed a good solution that maintains paging (which some of the answers here do not) and also allows for only targeting certain comment types and comment approved statuses.

function has_comments_join( $join, &$wp_query ) {
    global $wpdb;
    if ($comment_term = $wp_query->get('post_has_comments')) {
        $join .= $wpdb->prepare(" JOIN $wpdb->comments ON (($wpdb->posts.ID = $wpdb->comments.comment_post_ID) AND ($wpdb->comments.comment_type = %s) AND ($wpdb->comments.comment_approved = %s)) ", $comment_term['comment_type'], $comment_term['comment_status']);
    }
    return $join;
}

$results = new WP_Query(array(
    ...
    'post_has_comments' => array(
        'comment_type' => 'review',
        'comment_status' => '1'
    )
);

This code can easily be refined to support multiple statuses, conditionals on type and status, etc... I even have a variant I use to do comparisons on comment meta, similar to the meta_query functionality.

Enjoy!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.