0

I have a custom post type of 'films', and I'm creating a number of search filters using wp_query. All are working fine, but I now need to add in an option of exclude results that dont have any top level comments.

I was hoping it would be as simple as:

   $args = array(
       'post_type' => 'films',
       'comment_parent' => 0
    );

But this still returns all results. Is it possible to do what I want with wp_query?

EDIT

Here is my query so far. It searches for the 'films' post type and filters based on genre. Its a bit messy at the minute, but I just wanted to get everything working before tightening up the code.

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


    if(isset($_GET['g'])){
      $get_genres = $_GET['g'];

      $get_genres = array(
       'tax_query' => array(
            array(
               'taxonomy' => 'genre',
               'field' => 'id',
               'terms' => $get_genres,
               'operator' => 'IN'
            )
         )
      );


   $postsperpage = 10;
   $offset = $paged == 0 ? 0 : ($paged-1)*$postsperpage;

   $paging =   array(
      'posts_per_page' => $postsperpage,
      'offset' => $offset
   );

   $ordering =  array(
      'orderby' => 'title',
      'order' => 'ASC'
   );

   $args = array_merge($get_genres, $args);

   $args = array_merge($args, $ordering, $paging);  
   $wp_query = new WP_Query($args);
0

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.

  • Thanks Jacob, that makes sense. Do you know if it's possible to achieve what I'm after using a filter, similar to Brady's answer here: wordpress.stackexchange.com/questions/22949/query-post-by-title – Chris Mar 10 '13 at 19:10
  • Please post the full query you are using and I'll see if I can help. – Jacob Rambo Mar 10 '13 at 19:15
  • Will do. I'll add it to the original question. I'm a noob and its work in progress, so be gentle LOL – Chris Mar 10 '13 at 19:17
  • Chris see if this does the trick for you. – Jacob Rambo Mar 10 '13 at 21:34
  • Thats perfect, Jacob. Thank you :) I came up with a similar solution (eventually), though not as tidy as your code. But its good to know I was on the right track. Thanks again. – Chris Mar 10 '13 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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