WordPress: Post list without comments

I have a simple page on which I want to display a list of posts that have not yet been commented on. How can I do it? I assume some parameters can be added to query_posts? Thanks.

+4
source share
3 answers

Unfortunately, query_posts does not allow you to limit the query comment_count=0 . You can do it:

 query_posts( 'orderby=comment_count&order=ASC' ); 

But this not only displays posts with null comments, but also displays only those with zero comments.

A more attractive (but better) solution is to use a custom query that specifically restricts the query to messages with 0 comments, but that means you need to create your own loop structure (at least as far as I can tell )

 global $wpdb; $query = " SELECT * FROM {$wpdb->prefix}posts WHERE {$wpdb->prefix}posts.post_type = 'post' AND {$wpdb->prefix}posts.post_status = 'publish' AND {$wpdb->prefix}posts.comment_count = 0 ORDER BY {$wpdb->prefix}posts.post_date DESC; "; $pageposts = $wpdb->get_results($query, OBJECT); <?php if ($pageposts): ?> <?php global $post; ?> <?php foreach ($pageposts as $post): ?> <?php setup_postdata($post); ?> <div class="post" id="post-<?php the_ID(); ?>"> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> <?php the_title(); ?></a></h2> <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small> <div class="entry"> <?php the_content('Read the rest of this entry Β»'); ?> </div> <p class="postmetadata">Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments Β»', '1 Comment Β»', '% Comments Β»'); ?></p> </div> <?php endforeach; ?> <?php else : ?> <h2 class="center">Not Found</h2> <p class="center">Sorry, but you are looking for something that isn't here.</p> <?php include (TEMPLATEPATH . "/searchform.php"); ?> <?php endif; ?> 

Does this know what you know?

+3
source

You can configure the filter and varaible query to change the SQL that queries the messages. Add this to your theme functions.php file

 function filter_comment_count( $sql ){ global $wpdb; $comment_count = get_query_var( 'comment_count' ); if( is_numeric($comment_count) ) $sql .= $wpdb->prepare( " AND {$wpdb->posts}.comment_count = %d ", $comment_count ); return $sql; } 

Then you can call query_posts( 'comment_count=0' ); (any number), you just want to add a filter in advance,

 add_filter( 'posts_where', 'filter_comment_count' ); 

And after you make the call, you will also want to remove the filter.

 remove_filter( 'posts_where', 'filter_comment_count' ); 
+15
source

as simple as:

 query_posts( array ( 'post_type' => 'yourposttype', 'posts_per_page' => 10, 'comment_count' => 0, ) ); 
0
source

Source: https://habr.com/ru/post/1337492/


All Articles