wp_query and comment_parent – select only posts with top level comments

A post without a top level comment means it doesn’t have any comments at all. You can’t query posts without comments unless you have a custom $wpdb query.

You can however run a check against the post to see if it has comments and then display it using has_comments() if the $wp_query object is set.

You can read more about the has comments function here: http://codex.wordpress.org/Function_Reference/have_comments

You could do a custom query like this:

if(!empty($_GET['no_comments'])){

  global $wpdb;

  $post_ids= $wpdb->get_col( $wpdb->prepare( "
    SELECT ID FROM {$wpdb->posts}
    WHERE comment_count = %d AND post_type = %s", 0, 'films') );

   $args = array(
    'post_type' => 'films',
    'post__in'    =>  $post_ids
   );

} else {

 $args = array(
    'post_type' => 'films'
   );

}

This assumes you are using a form to choose whether or not to show posts with comments.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)