WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Query only posts with active comments (11 posts)

  1. levani01
    Member
    Posted 4 years ago #

    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?

  2. MichaelH
    Member
    Posted 4 years ago #

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

    if ( 'open' == $post->comment_status ) {
    
      LOOP
    
    }
  3. levani01
    Member
    Posted 4 years ago #

    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!

  4. MichaelH
    Member
    Posted 4 years ago #

    It should be

    $posts->comment_status

    not

    $post->comment_status

    Sorry

  5. levani01
    Member
    Posted 4 years ago #

    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;  ?>
  6. MichaelH
    Member
    Posted 4 years ago #

    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

  7. levani01
    Member
    Posted 4 years ago #

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

  8. MichaelH
    Member
    Posted 4 years ago #

    See Pages for example(s).

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

  9. levani01
    Member
    Posted 4 years ago #

    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?

  10. MichaelH
    Member
    Posted 4 years ago #

    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.

  11. fleshins
    Member
    Posted 4 years ago #

    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 :)

Topic Closed

This topic has been closed to new replies.

About this Topic