Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I previously asked and accepted a similar question here: wp_query and comment_parent - select only posts with top level comments

So I'm trying to use wp_query to select only posts that have comments. The problem I'm facing now is I have numerous filters as part of an advanced search e.g. name, location (country, area), tags etc etc and it is becoming to difficult to select post ids in the method shown in the accepted answer. There are just to many possible combinations of searches and options.

Ideally I need a way to exclude posts without a comment globally, in one hit.

I was hoping for something nice and easy like:

 $args = array(
       'post_type' => 'films',
       'have_comments' => true
    );

But I cant seem to find anything like that.

I thought maybe a filter in functions.php or with the use of have_comments() somehow? I dont really want to select all posts and hide in the have posts loop if I can help it as there could potentially be hundreds of rows returned in wp_query that are not needed.

Sorry, I'm a beginner at this so I'm not really sure of a way forward.

share|improve this question
    
stackoverflow.com/questions/4829733/… - Look at the seconds answer, you just need to change the IF statement –  passatgt Apr 1 '13 at 11:08
    
Hi passatgt. Do you mean answer 1 "if(!empty($_GET['no_comments'])){"? if so, change it to what? –  Chris Apr 1 '13 at 11:24
add comment

1 Answer

up vote 0 down vote accepted

You might try this:

add_filter( 'posts_where', 'filter_where' );
$query = new WP_Query($args);
remove_filter( 'posts_where', 'filter_where' );

where

function filter_where( $where = '' ) {
    $where .= " AND comment_count > 0 ";
    return $where;
}

to get posts with comments.

share|improve this answer
    
Thanks ever so much, birgire. You're a lifesaver! –  Chris Apr 1 '13 at 11:41
add comment

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.