Title: Query only posts with active comments
Last modified: August 19, 2016

---

# Query only posts with active comments

 *  Resolved [levani01](https://wordpress.org/support/users/levani01/)
 * (@levani01)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/query-only-posts-with-active-comments/)
 * How should I modify this code to query only posts where comment status is active?(
   Comment status automatically become closed after 20 days on my site, so I need
   to query posts only with active comments.)
 *     ```
       <?php
         $myqueryname = $wp_query;
         $wp_query = null;
         $wp_query = new WP_Query();
         $wp_query->query('author='1'&post_status=publish');
       ?>
         <?php if (have_posts()) : ?>
           <?php while (have_posts()) : the_post(); ?>
   
         LOOP
   
           <?php endwhile; ?>
         <?php else : ?>
        <h1>No results.</h1>
         <?php endif; ?>
   
       <?php $wp_query = null; $wp_query = $myqueryname;?>
       ```
   
 * Can anyone please help?

Viewing 10 replies - 1 through 10 (of 10 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/query-only-posts-with-active-comments/#post-1202359)
 * Don’t think query_posts will let you filter on the comment_status so what about:
 *     ```
       if ( 'open' == $post->comment_status ) {
   
         LOOP
   
       }
       ```
   
 *  Thread Starter [levani01](https://wordpress.org/support/users/levani01/)
 * (@levani01)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/query-only-posts-with-active-comments/#post-1202390)
 * I can’t make it work…
 *     ```
       <?php if (have_posts()) : ?>
   
       	    <?php while (have_posts()) : the_post(); ?>
   
       		<?php if ( 'open' == $post->comment_status ) { ?>
   
       		  <div class="post" id="post-<?php the_ID(); ?>">
   
       			<div class="satauri"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div><p class="post-footer align-left"><?php the_category(', ') ?> | Author: <?php the_author() ?></p>
       <div style="clear:both"></div>
               </div>
   
       	<?php
       		}
       		endwhile;
       		?>	
   
       	    <?php else : ?>
   
            	<h1>No results.</h1>
   
       	    <?php endif;  ?>
       ```
   
 * I don’t know why but it displays only one newest page, not what I said above…
 * I forgot to say, I’m trying to run this code on a custom page template. Can this
   cause any problems?
 * Any help would be appreciated!
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/query-only-posts-with-active-comments/#post-1202399)
 * It should be
 * $posts->comment_status
 * not
 * $post->comment_status
 * Sorry
 *  Thread Starter [levani01](https://wordpress.org/support/users/levani01/)
 * (@levani01)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/query-only-posts-with-active-comments/#post-1202486)
 * Sorry, but it doesn’t solved the problem. This code doesn’t even query anything!
 *     ```
       <?php if (have_posts()) : ?>
   
       	    <?php while (have_posts()) : the_post(); ?>
   
       		<?php if ( 'open' == $posts->comment_status ) { ?>
   
       		  <div class="post" id="post-<?php the_ID(); ?>">
   
       			<div class="satauri"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div><p class="post-footer align-left"><?php the_category(', ') ?> | Author: <?php the_author() ?></p>
       <div style="clear:both"></div>
               </div>
   
       	<?php
       		}
       		endwhile;
       		?>	
   
       	    <?php else : ?>
   
            	<h1>No results.</h1>
   
       	    <?php endif;  ?>
       ```
   
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/query-only-posts-with-active-comments/#post-1202498)
 * Sorry–I was wrong before. This works in my loop:
 *     ```
       echo 'comment status is :' . $post->comment_status;
       ```
   
 * So you should be able to test $post->comment_status
 *  Thread Starter [levani01](https://wordpress.org/support/users/levani01/)
 * (@levani01)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/query-only-posts-with-active-comments/#post-1202511)
 * Does the standard wordpress loop work on custom page template, without query_posts
   function?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/query-only-posts-with-active-comments/#post-1202512)
 * See [Pages](http://codex.wordpress.org/Pages) for example(s).
 * Note: Please don’t forget to mark threads resolved when the topic is completed.
 *  Thread Starter [levani01](https://wordpress.org/support/users/levani01/)
 * (@levani01)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/query-only-posts-with-active-comments/#post-1202533)
 * I noticed a very strange behavior of $post->comment_status; function. I tried
   inserting the
 * `echo 'comment status is :' . $post->comment_status;`
 * code and it always displays ‘open’ even if the post is published more than several
   months ago (Comments become closed automatically after 20 days on my site on 
   every posts).
 * I also checked the database entries and in wp_posts table, under the comment_status
   column, all values are ‘open’!!!
 * But the most strange thing is that in single.php, comparing ‘open’ to $post->
   comment_status successfully works and removes the comment form if it doesn’t 
   match…
 * Can you please explain why does it happen?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 8 months ago](https://wordpress.org/support/topic/query-only-posts-with-active-comments/#post-1202541)
 * Well this is interesting.
 * When the “Automatically close comments feature” is in effect, the wp_posts comment_status
   field does not change in the database, but rather is determined, on the fly, 
   by the comments_open() function, which has the `_close_comments_for_old_post()`
   function applied via the “comments_open” filter.
 * So using something like the example in the wp-content/themes/default/single.php,
   is a simple solution to your problem:
 *     ```
       <?php if ( comments_open() && pings_open() ) {
       // Both Comments and Pings are open ?>
       ```
   
 * As for the difference of `echo 'comment status is :' . $post->comment_status;`
   between the index.php and single.php [Templates](http://codex.wordpress.org/Templates),
   the actual value of $post->comment_status field gets set, on the fly, and not
   changed in the database, when the `_close_comments_for_old_posts()` function 
   is applied to the posts via the “the_posts” filter in query.php, which tests `
   is_singular()` to “happen”.
 * Note that the database does get changed to “closed” if the “Allow comments on
   this post” in Add/Edit Post is unchecked.
 *  [fleshins](https://wordpress.org/support/users/fleshins/)
 * (@fleshins)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/query-only-posts-with-active-comments/#post-1202640)
 * why would you want to change an object’s property (or anything for that matter)
   but not write it to the DB? doesn’t this mean that if the object cache is flushed–
   you’ll lose your changes?
 * thanks for letting me know what i’m missing 🙂

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Query only posts with active comments’ is closed to new replies.

## Tags

 * [active](https://wordpress.org/support/topic-tag/active/)
 * [Comments](https://wordpress.org/support/topic-tag/comments/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [status](https://wordpress.org/support/topic-tag/status/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 10 replies
 * 3 participants
 * Last reply from: [fleshins](https://wordpress.org/support/users/fleshins/)
 * Last activity: [16 years, 3 months ago](https://wordpress.org/support/topic/query-only-posts-with-active-comments/#post-1202640)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
