WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Query only posts with active comments

[Resolved] Query only posts with active comments

  • levani01

    @levani01

    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

    @michaelh

    Don’t think query_posts will let you filter on the comment_status so what about:

    if ( 'open' == $post->comment_status ) {
    
      LOOP
    
    }
    levani01

    @levani01

    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

    @michaelh

    It should be

    $posts->comment_status

    not

    $post->comment_status

    Sorry

    levani01

    @levani01

    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

    @michaelh

    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

    levani01

    @levani01

    Does the standard wordpress loop work on custom page template, without query_posts function?

    MichaelH

    @michaelh

    See Pages for example(s).

    Note: Please don’t forget to mark threads resolved when the topic is completed.

    levani01

    @levani01

    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

    @michaelh

    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, 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.

    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 ‘[Resolved] Query only posts with active comments’ is closed to new replies.